smp: add func to IPI cpus based on parameter func
[linux-flexiantxendom0-3.2.10.git] / kernel / params.c
index a550698..47f5bf1 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
-#include <linux/moduleparam.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/ctype.h>
 
-#if 0
-#define DEBUGP printk
-#else
-#define DEBUGP(fmt, a...)
-#endif
+/* Protects all parameters, and incidentally kmalloced_param list. */
+static DEFINE_MUTEX(param_lock);
+
+/* This just allows us to keep track of which parameters are kmalloced. */
+struct kmalloced_param {
+       struct list_head list;
+       char val[];
+};
+static LIST_HEAD(kmalloced_params);
+
+static void *kmalloc_parameter(unsigned int size)
+{
+       struct kmalloced_param *p;
 
-static inline char dash2underscore(char c)
+       p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
+       if (!p)
+               return NULL;
+
+       list_add(&p->list, &kmalloced_params);
+       return p->val;
+}
+
+/* Does nothing if parameter wasn't kmalloced above. */
+static void maybe_kfree_parameter(void *param)
+{
+       struct kmalloced_param *p;
+
+       list_for_each_entry(p, &kmalloced_params, list) {
+               if (p->val == param) {
+                       list_del(&p->list);
+                       kfree(p);
+                       break;
+               }
+       }
+}
+
+static char dash2underscore(char c)
 {
        if (c == '-')
                return '_';
        return c;
 }
 
-static inline int parameq(const char *input, const char *paramname)
+bool parameqn(const char *a, const char *b, size_t n)
 {
-       unsigned int i;
-       for (i = 0; dash2underscore(input[i]) == paramname[i]; i++)
-               if (input[i] == '\0')
-                       return 1;
-       return 0;
+       size_t i;
+
+       for (i = 0; i < n; i++) {
+               if (dash2underscore(a[i]) != dash2underscore(b[i]))
+                       return false;
+       }
+       return true;
+}
+
+bool parameq(const char *a, const char *b)
+{
+       return parameqn(a, b, strlen(a)+1);
 }
 
 static int parse_one(char *param,
                     char *val,
-                    struct kernel_param *params, 
+                    const struct kernel_param *params,
                     unsigned num_params,
                     int (*handle_unknown)(char *param, char *val))
 {
        unsigned int i;
+       int err;
 
        /* Find parameter */
        for (i = 0; i < num_params; i++) {
                if (parameq(param, params[i].name)) {
-                       /* Noone handled NULL, so do it here. */
-                       if (!val && params[i].ops->set != param_set_bool)
+                       /* No one handled NULL, so do it here. */
+                       if (!val && params[i].ops->set != param_set_bool
+                           && params[i].ops->set != param_set_bint)
                                return -EINVAL;
-                       DEBUGP("They are equal!  Calling %p\n",
+                       pr_debug("They are equal!  Calling %p\n",
                               params[i].ops->set);
-                       return params[i].ops->set(val, &params[i]);
+                       mutex_lock(&param_lock);
+                       err = params[i].ops->set(val, &params[i]);
+                       mutex_unlock(&param_lock);
+                       return err;
                }
        }
 
        if (handle_unknown) {
-               DEBUGP("Unknown argument: calling %p\n", handle_unknown);
+               pr_debug("Unknown argument: calling %p\n", handle_unknown);
                return handle_unknown(param, val);
        }
 
-       DEBUGP("Unknown argument `%s'\n", param);
+       pr_debug("Unknown argument `%s'\n", param);
        return -ENOENT;
 }
 
@@ -131,13 +172,13 @@ static char *next_arg(char *args, char **param, char **val)
 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
 int parse_args(const char *name,
               char *args,
-              struct kernel_param *params,
+              const struct kernel_param *params,
               unsigned num,
               int (*unknown)(char *param, char *val))
 {
        char *param, *val;
 
-       DEBUGP("Parsing ARGS: %s\n", args);
+       pr_debug("Parsing ARGS: %s\n", args);
 
        /* Chew leading spaces */
        args = skip_spaces(args);
@@ -185,8 +226,8 @@ int parse_args(const char *name,
                int ret;                                                \
                                                                        \
                ret = strtolfn(val, 0, &l);                             \
-               if (ret == -EINVAL || ((type)l != l))                   \
-                       return -EINVAL;                                 \
+               if (ret < 0 || ((type)l != l))                          \
+                       return ret < 0 ? ret : -EINVAL;                 \
                *((type *)kp->arg) = l;                                 \
                return 0;                                               \
        }                                                               \
@@ -219,12 +260,15 @@ int param_set_charp(const char *val, const struct kernel_param *kp)
                return -ENOSPC;
        }
 
-       /* This is a hack.  We can't need to strdup in early boot, and we
+       maybe_kfree_parameter(*(char **)kp->arg);
+
+       /* This is a hack.  We can't kmalloc in early boot, and we
         * don't need to; this mangled commandline is preserved. */
        if (slab_is_available()) {
-               *(char **)kp->arg = kstrdup(val, GFP_KERNEL);
+               *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
                if (!*(char **)kp->arg)
                        return -ENOMEM;
+               strcpy(*(char **)kp->arg, val);
        } else
                *(const char **)kp->arg = val;
 
@@ -238,9 +282,15 @@ int param_get_charp(char *buffer, const struct kernel_param *kp)
 }
 EXPORT_SYMBOL(param_get_charp);
 
+static void param_free_charp(void *arg)
+{
+       maybe_kfree_parameter(*((char **)arg));
+}
+
 struct kernel_param_ops param_ops_charp = {
        .set = param_set_charp,
        .get = param_get_charp,
+       .free = param_free_charp,
 };
 EXPORT_SYMBOL(param_ops_charp);
 
@@ -248,21 +298,15 @@ EXPORT_SYMBOL(param_ops_charp);
 int param_set_bool(const char *val, const struct kernel_param *kp)
 {
        bool v;
+       int ret;
 
        /* No equals means "set"... */
        if (!val) val = "1";
 
        /* One of =[yYnN01] */
-       switch (val[0]) {
-       case 'y': case 'Y': case '1':
-               v = true;
-               break;
-       case 'n': case 'N': case '0':
-               v = false;
-               break;
-       default:
-               return -EINVAL;
-       }
+       ret = strtobool(val, &v);
+       if (ret)
+               return ret;
 
        if (kp->flags & KPARAM_ISBOOL)
                *(bool *)kp->arg = v;
@@ -319,6 +363,30 @@ struct kernel_param_ops param_ops_invbool = {
 };
 EXPORT_SYMBOL(param_ops_invbool);
 
+int param_set_bint(const char *val, const struct kernel_param *kp)
+{
+       struct kernel_param boolkp;
+       bool v;
+       int ret;
+
+       /* Match bool exactly, by re-using it. */
+       boolkp = *kp;
+       boolkp.arg = &v;
+       boolkp.flags |= KPARAM_ISBOOL;
+
+       ret = param_set_bool(val, &boolkp);
+       if (ret == 0)
+               *(int *)kp->arg = v;
+       return ret;
+}
+EXPORT_SYMBOL(param_set_bint);
+
+struct kernel_param_ops param_ops_bint = {
+       .set = param_set_bint,
+       .get = param_get_int,
+};
+EXPORT_SYMBOL(param_ops_bint);
+
 /* We break the rule and mangle the string. */
 static int param_array(const char *name,
                       const char *val,
@@ -352,6 +420,7 @@ static int param_array(const char *name,
                /* nul-terminate and parse */
                save = val[len];
                ((char *)val)[len] = '\0';
+               BUG_ON(!mutex_is_locked(&param_lock));
                ret = set(val, &kp);
 
                if (ret != 0)
@@ -390,6 +459,7 @@ static int param_array_get(char *buffer, const struct kernel_param *kp)
                if (i)
                        buffer[off++] = ',';
                p.arg = arr->elem + arr->elemsize * i;
+               BUG_ON(!mutex_is_locked(&param_lock));
                ret = arr->ops->get(buffer + off, &p);
                if (ret < 0)
                        return ret;
@@ -399,9 +469,20 @@ static int param_array_get(char *buffer, const struct kernel_param *kp)
        return off;
 }
 
+static void param_array_free(void *arg)
+{
+       unsigned int i;
+       const struct kparam_array *arr = arg;
+
+       if (arr->ops->free)
+               for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
+                       arr->ops->free(arr->elem + arr->elemsize * i);
+}
+
 struct kernel_param_ops param_array_ops = {
        .set = param_array_set,
        .get = param_array_get,
+       .free = param_array_free,
 };
 EXPORT_SYMBOL(param_array_ops);
 
@@ -455,7 +536,7 @@ struct module_param_attrs
 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
 
 static ssize_t param_attr_show(struct module_attribute *mattr,
-                              struct module *mod, char *buf)
+                              struct module_kobject *mk, char *buf)
 {
        int count;
        struct param_attribute *attribute = to_param_attr(mattr);
@@ -463,7 +544,9 @@ static ssize_t param_attr_show(struct module_attribute *mattr,
        if (!attribute->param->ops->get)
                return -EPERM;
 
+       mutex_lock(&param_lock);
        count = attribute->param->ops->get(buf, attribute->param);
+       mutex_unlock(&param_lock);
        if (count > 0) {
                strcat(buf, "\n");
                ++count;
@@ -473,7 +556,7 @@ static ssize_t param_attr_show(struct module_attribute *mattr,
 
 /* sysfs always hands a nul-terminated string in buf.  We rely on that. */
 static ssize_t param_attr_store(struct module_attribute *mattr,
-                               struct module *owner,
+                               struct module_kobject *km,
                                const char *buf, size_t len)
 {
        int err;
@@ -482,7 +565,9 @@ static ssize_t param_attr_store(struct module_attribute *mattr,
        if (!attribute->param->ops->set)
                return -EPERM;
 
+       mutex_lock(&param_lock);
        err = attribute->param->ops->set(buf, attribute->param);
+       mutex_unlock(&param_lock);
        if (!err)
                return len;
        return err;
@@ -496,6 +581,18 @@ static ssize_t param_attr_store(struct module_attribute *mattr,
 #endif
 
 #ifdef CONFIG_SYSFS
+void __kernel_param_lock(void)
+{
+       mutex_lock(&param_lock);
+}
+EXPORT_SYMBOL(__kernel_param_lock);
+
+void __kernel_param_unlock(void)
+{
+       mutex_unlock(&param_lock);
+}
+EXPORT_SYMBOL(__kernel_param_unlock);
+
 /*
  * add_sysfs_param - add a parameter to sysfs
  * @mk: struct module_kobject
@@ -634,12 +731,14 @@ void module_param_sysfs_remove(struct module *mod)
 
 void destroy_params(const struct kernel_param *params, unsigned num)
 {
-       /* FIXME: This should free kmalloced charp parameters.  It doesn't. */
+       unsigned int i;
+
+       for (i = 0; i < num; i++)
+               if (params[i].ops->free)
+                       params[i].ops->free(params[i].arg);
 }
 
-static void __init kernel_add_sysfs_param(const char *name,
-                                         struct kernel_param *kparam,
-                                         unsigned int name_skip)
+static struct module_kobject * __init locate_module_kobject(const char *name)
 {
        struct module_kobject *mk;
        struct kobject *kobj;
@@ -647,10 +746,7 @@ static void __init kernel_add_sysfs_param(const char *name,
 
        kobj = kset_find_obj(module_kset, name);
        if (kobj) {
-               /* We already have one.  Remove params so we can add more. */
                mk = to_module_kobject(kobj);
-               /* We need to remove it before adding parameters. */
-               sysfs_remove_group(&mk->kobj, &mk->mp->grp);
        } else {
                mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
                BUG_ON(!mk);
@@ -659,17 +755,42 @@ static void __init kernel_add_sysfs_param(const char *name,
                mk->kobj.kset = module_kset;
                err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
                                           "%s", name);
+#ifdef CONFIG_MODULES
+               if (!err)
+                       err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
+#endif
                if (err) {
                        kobject_put(&mk->kobj);
-                       printk(KERN_ERR "Module '%s' failed add to sysfs, "
-                              "error number %d\n", name, err);
-                       printk(KERN_ERR "The system will be unstable now.\n");
-                       return;
+                       printk(KERN_ERR
+                               "Module '%s' failed add to sysfs, error number %d\n",
+                               name, err);
+                       printk(KERN_ERR
+                               "The system will be unstable now.\n");
+                       return NULL;
                }
-               /* So that exit path is even. */
+
+               /* So that we hold reference in both cases. */
                kobject_get(&mk->kobj);
        }
 
+       return mk;
+}
+
+static void __init kernel_add_sysfs_param(const char *name,
+                                         struct kernel_param *kparam,
+                                         unsigned int name_skip)
+{
+       struct module_kobject *mk;
+       int err;
+
+       mk = locate_module_kobject(name);
+       if (!mk)
+               return;
+
+       /* We need to remove old parameters before adding more. */
+       if (mk->mp)
+               sysfs_remove_group(&mk->kobj, &mk->mp->grp);
+
        /* These should not fail at boot. */
        err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
        BUG_ON(err);
@@ -714,6 +835,35 @@ static void __init param_sysfs_builtin(void)
        }
 }
 
+ssize_t __modver_version_show(struct module_attribute *mattr,
+                             struct module_kobject *mk, char *buf)
+{
+       struct module_version_attribute *vattr =
+               container_of(mattr, struct module_version_attribute, mattr);
+
+       return sprintf(buf, "%s\n", vattr->version);
+}
+
+extern const struct module_version_attribute *__start___modver[];
+extern const struct module_version_attribute *__stop___modver[];
+
+static void __init version_sysfs_builtin(void)
+{
+       const struct module_version_attribute **p;
+       struct module_kobject *mk;
+       int err;
+
+       for (p = __start___modver; p < __stop___modver; p++) {
+               const struct module_version_attribute *vattr = *p;
+
+               mk = locate_module_kobject(vattr->module_name);
+               if (mk) {
+                       err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
+                       kobject_uevent(&mk->kobj, KOBJ_ADD);
+                       kobject_put(&mk->kobj);
+               }
+       }
+}
 
 /* module-related sysfs stuff */
 
@@ -731,7 +881,7 @@ static ssize_t module_attr_show(struct kobject *kobj,
        if (!attribute->show)
                return -EIO;
 
-       ret = attribute->show(attribute, mk->mod, buf);
+       ret = attribute->show(attribute, mk, buf);
 
        return ret;
 }
@@ -750,7 +900,7 @@ static ssize_t module_attr_store(struct kobject *kobj,
        if (!attribute->store)
                return -EIO;
 
-       ret = attribute->store(attribute, mk->mod, buf, len);
+       ret = attribute->store(attribute, mk, buf, len);
 
        return ret;
 }
@@ -793,6 +943,7 @@ static int __init param_sysfs_init(void)
        }
        module_sysfs_initialized = 1;
 
+       version_sysfs_builtin();
        param_sysfs_builtin();
 
        return 0;