[S390] topology: move topology sysinfo code
[pandora-kernel.git] / kernel / params.c
index a550698..08107d1 100644 (file)
 #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;
+
+       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 inline char dash2underscore(char c)
 {
        if (c == '-')
@@ -49,11 +85,12 @@ static inline int parameq(const char *input, const char *paramname)
 
 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++) {
@@ -63,7 +100,10 @@ static int parse_one(char *param,
                                return -EINVAL;
                        DEBUGP("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;
                }
        }
 
@@ -131,7 +171,7 @@ 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))
 {
@@ -219,12 +259,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 +281,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);
 
@@ -352,6 +401,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 +440,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 +450,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);
 
@@ -463,7 +525,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;
@@ -482,7 +546,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 +562,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,7 +712,11 @@ 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,