Commit 9b473de8 authored by Rusty Russell's avatar Rusty Russell

param: Fix duplicate module prefixes

Instead of insisting each new module_param sysfs entry is unique,
handle the case where it already exists (for builtin modules).

The current code assumes that all identical prefixes are together in
the section: true for normal uses, but not necessarily so if someone
overrides MODULE_PARAM_PREFIX.  More importantly, it's not true with
the new "core_param()" code which uses "kernel" as a prefix.

This simplifies the caller for the builtin case, at a slight loss of
efficiency (we do the lookup every time to see if the directory
exists).
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
Cc: Greg Kroah-Hartman <gregkh@suse.de>
parent 730b69d2
...@@ -60,6 +60,7 @@ struct module_kobject ...@@ -60,6 +60,7 @@ struct module_kobject
struct kobject kobj; struct kobject kobj;
struct module *mod; struct module *mod;
struct kobject *drivers_dir; struct kobject *drivers_dir;
struct module_param_attrs *mp;
}; };
/* These are either module local, or the kernel's dummy ones. */ /* These are either module local, or the kernel's dummy ones. */
...@@ -242,7 +243,6 @@ struct module ...@@ -242,7 +243,6 @@ struct module
/* Sysfs stuff. */ /* Sysfs stuff. */
struct module_kobject mkobj; struct module_kobject mkobj;
struct module_param_attrs *param_attrs;
struct module_attribute *modinfo_attrs; struct module_attribute *modinfo_attrs;
const char *version; const char *version;
const char *srcversion; const char *srcversion;
......
...@@ -373,6 +373,8 @@ int param_get_string(char *buffer, struct kernel_param *kp) ...@@ -373,6 +373,8 @@ int param_get_string(char *buffer, struct kernel_param *kp)
} }
/* sysfs output in /sys/modules/XYZ/parameters/ */ /* sysfs output in /sys/modules/XYZ/parameters/ */
#define to_module_attr(n) container_of(n, struct module_attribute, attr);
#define to_module_kobject(n) container_of(n, struct module_kobject, kobj);
extern struct kernel_param __start___param[], __stop___param[]; extern struct kernel_param __start___param[], __stop___param[];
...@@ -384,6 +386,7 @@ struct param_attribute ...@@ -384,6 +386,7 @@ struct param_attribute
struct module_param_attrs struct module_param_attrs
{ {
unsigned int num;
struct attribute_group grp; struct attribute_group grp;
struct param_attribute attrs[0]; struct param_attribute attrs[0];
}; };
...@@ -434,69 +437,84 @@ static ssize_t param_attr_store(struct module_attribute *mattr, ...@@ -434,69 +437,84 @@ static ssize_t param_attr_store(struct module_attribute *mattr,
#ifdef CONFIG_SYSFS #ifdef CONFIG_SYSFS
/* /*
* param_sysfs_setup - setup sysfs support for one module or KBUILD_MODNAME * add_sysfs_param - add a parameter to sysfs
* @mk: struct module_kobject (contains parent kobject) * @mk: struct module_kobject
* @kparam: array of struct kernel_param, the actual parameter definitions * @kparam: the actual parameter definition to add to sysfs
* @num_params: number of entries in array * @name: name of parameter
* @name_skip: offset where the parameter name start in kparam[].name. Needed for built-in "modules"
* *
* Create a kobject for a (per-module) group of parameters, and create files * Create a kobject if for a (per-module) parameter if mp NULL, and
* in sysfs. A pointer to the param_kobject is returned on success, * create file in sysfs. Returns an error on out of memory. Always cleans up
* NULL if there's no parameter to export, or other ERR_PTR(err). * if there's an error.
*/ */
static __modinit struct module_param_attrs * static __modinit int add_sysfs_param(struct module_kobject *mk,
param_sysfs_setup(struct module_kobject *mk, struct kernel_param *kp,
struct kernel_param *kparam, const char *name)
unsigned int num_params,
unsigned int name_skip)
{ {
struct module_param_attrs *mp; struct module_param_attrs *new;
unsigned int valid_attrs = 0; struct attribute **attrs;
unsigned int i, size[2]; int err, num;
struct param_attribute *pattr;
struct attribute **gattr; /* We don't bother calling this with invisible parameters. */
int err; BUG_ON(!kp->perm);
for (i=0; i<num_params; i++) { if (!mk->mp) {
if (kparam[i].perm) num = 0;
valid_attrs++; attrs = NULL;
} } else {
num = mk->mp->num;
if (!valid_attrs) attrs = mk->mp->grp.attrs;
return NULL; }
size[0] = ALIGN(sizeof(*mp) + /* Enlarge. */
valid_attrs * sizeof(mp->attrs[0]), new = krealloc(mk->mp,
sizeof(mp->grp.attrs[0])); sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
size[1] = (valid_attrs + 1) * sizeof(mp->grp.attrs[0]); GFP_KERNEL);
if (!new) {
mp = kzalloc(size[0] + size[1], GFP_KERNEL); kfree(mk->mp);
if (!mp) err = -ENOMEM;
return ERR_PTR(-ENOMEM); goto fail;
}
mp->grp.name = "parameters"; attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
mp->grp.attrs = (void *)mp + size[0]; if (!attrs) {
err = -ENOMEM;
goto fail_free_new;
}
/* Sysfs wants everything zeroed. */
memset(new, 0, sizeof(*new));
memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
memset(&attrs[num], 0, sizeof(attrs[num]));
new->grp.name = "parameters";
new->grp.attrs = attrs;
/* Tack new one on the end. */
new->attrs[num].param = kp;
new->attrs[num].mattr.show = param_attr_show;
new->attrs[num].mattr.store = param_attr_store;
new->attrs[num].mattr.attr.name = (char *)name;
new->attrs[num].mattr.attr.mode = kp->perm;
new->num = num+1;
/* Fix up all the pointers, since krealloc can move us */
for (num = 0; num < new->num; num++)
new->grp.attrs[num] = &new->attrs[num].mattr.attr;
new->grp.attrs[num] = NULL;
mk->mp = new;
return 0;
pattr = &mp->attrs[0]; fail_free_new:
gattr = &mp->grp.attrs[0]; kfree(new);
for (i = 0; i < num_params; i++) { fail:
struct kernel_param *kp = &kparam[i]; mk->mp = NULL;
if (kp->perm) { return err;
pattr->param = kp; }
pattr->mattr.show = param_attr_show;
pattr->mattr.store = param_attr_store;
pattr->mattr.attr.name = (char *)&kp->name[name_skip];
pattr->mattr.attr.mode = kp->perm;
*(gattr++) = &(pattr++)->mattr.attr;
}
}
*gattr = NULL;
if ((err = sysfs_create_group(&mk->kobj, &mp->grp))) { static void free_module_param_attrs(struct module_kobject *mk)
kfree(mp); {
return ERR_PTR(err); kfree(mk->mp->grp.attrs);
} kfree(mk->mp);
return mp; mk->mp = NULL;
} }
#ifdef CONFIG_MODULES #ifdef CONFIG_MODULES
...@@ -506,21 +524,33 @@ param_sysfs_setup(struct module_kobject *mk, ...@@ -506,21 +524,33 @@ param_sysfs_setup(struct module_kobject *mk,
* @kparam: module parameters (array) * @kparam: module parameters (array)
* @num_params: number of module parameters * @num_params: number of module parameters
* *
* Adds sysfs entries for module parameters, and creates a link from * Adds sysfs entries for module parameters under
* /sys/module/[mod->name]/parameters to /sys/parameters/[mod->name]/ * /sys/module/[mod->name]/parameters/
*/ */
int module_param_sysfs_setup(struct module *mod, int module_param_sysfs_setup(struct module *mod,
struct kernel_param *kparam, struct kernel_param *kparam,
unsigned int num_params) unsigned int num_params)
{ {
struct module_param_attrs *mp; int i, err;
bool params = false;
mp = param_sysfs_setup(&mod->mkobj, kparam, num_params, 0); for (i = 0; i < num_params; i++) {
if (IS_ERR(mp)) if (kparam[i].perm == 0)
return PTR_ERR(mp); continue;
err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
if (err)
return err;
params = true;
}
mod->param_attrs = mp; if (!params)
return 0; return 0;
/* Create the param group. */
err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
if (err)
free_module_param_attrs(&mod->mkobj);
return err;
} }
/* /*
...@@ -532,43 +562,55 @@ int module_param_sysfs_setup(struct module *mod, ...@@ -532,43 +562,55 @@ int module_param_sysfs_setup(struct module *mod,
*/ */
void module_param_sysfs_remove(struct module *mod) void module_param_sysfs_remove(struct module *mod)
{ {
if (mod->param_attrs) { if (mod->mkobj.mp) {
sysfs_remove_group(&mod->mkobj.kobj, sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
&mod->param_attrs->grp);
/* We are positive that no one is using any param /* We are positive that no one is using any param
* attrs at this point. Deallocate immediately. */ * attrs at this point. Deallocate immediately. */
kfree(mod->param_attrs); free_module_param_attrs(&mod->mkobj);
mod->param_attrs = NULL;
} }
} }
#endif #endif
/* static void __init kernel_add_sysfs_param(const char *name,
* kernel_param_sysfs_setup - wrapper for built-in params support
*/
static void __init kernel_param_sysfs_setup(const char *name,
struct kernel_param *kparam, struct kernel_param *kparam,
unsigned int num_params,
unsigned int name_skip) unsigned int name_skip)
{ {
struct module_kobject *mk; struct module_kobject *mk;
int ret; struct kobject *kobj;
int err;
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); mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
BUG_ON(!mk); BUG_ON(!mk);
mk->mod = THIS_MODULE; mk->mod = THIS_MODULE;
mk->kobj.kset = module_kset; mk->kobj.kset = module_kset;
ret = kobject_init_and_add(&mk->kobj, &module_ktype, NULL, "%s", name); err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
if (ret) { "%s", name);
if (err) {
kobject_put(&mk->kobj); kobject_put(&mk->kobj);
printk(KERN_ERR "Module '%s' failed to be added to sysfs, " printk(KERN_ERR "Module '%s' failed add to sysfs, "
"error number %d\n", name, ret); "error number %d\n", name, err);
printk(KERN_ERR "The system will be unstable now.\n"); printk(KERN_ERR "The system will be unstable now.\n");
return; return;
} }
param_sysfs_setup(mk, kparam, num_params, name_skip); /* So that exit path is even. */
kobject_get(&mk->kobj);
}
/* These should not fail at boot. */
err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
BUG_ON(err);
err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
BUG_ON(err);
kobject_uevent(&mk->kobj, KOBJ_ADD); kobject_uevent(&mk->kobj, KOBJ_ADD);
kobject_put(&mk->kobj);
} }
/* /*
...@@ -579,18 +621,19 @@ static void __init kernel_param_sysfs_setup(const char *name, ...@@ -579,18 +621,19 @@ static void __init kernel_param_sysfs_setup(const char *name,
* The "module" name (KBUILD_MODNAME) is stored before a dot, the * The "module" name (KBUILD_MODNAME) is stored before a dot, the
* "parameter" name is stored behind a dot in kernel_param->name. So, * "parameter" name is stored behind a dot in kernel_param->name. So,
* extract the "module" name for all built-in kernel_param-eters, * extract the "module" name for all built-in kernel_param-eters,
* and for all who have the same, call kernel_param_sysfs_setup. * and for all who have the same, call kernel_add_sysfs_param.
*/ */
static void __init param_sysfs_builtin(void) static void __init param_sysfs_builtin(void)
{ {
struct kernel_param *kp, *kp_begin = NULL; struct kernel_param *kp;
unsigned int i, name_len, count = 0; unsigned int name_len;
char modname[MODULE_NAME_LEN] = ""; char modname[MODULE_NAME_LEN];
for (i=0; i < __stop___param - __start___param; i++) { for (kp = __start___param; kp < __stop___param; kp++) {
char *dot; char *dot;
kp = &__start___param[i]; if (kp->perm == 0)
continue;
dot = strchr(kp->name, '.'); dot = strchr(kp->name, '.');
if (!dot) { if (!dot) {
...@@ -599,37 +642,15 @@ static void __init param_sysfs_builtin(void) ...@@ -599,37 +642,15 @@ static void __init param_sysfs_builtin(void)
continue; continue;
} }
name_len = dot - kp->name; name_len = dot - kp->name;
/* new kbuild_modname? */
if (strlen(modname) != name_len
|| strncmp(modname, kp->name, name_len) != 0) {
/* add a new kobject for previous kernel_params. */
if (count)
kernel_param_sysfs_setup(modname,
kp_begin,
count,
strlen(modname)+1);
strncpy(modname, kp->name, name_len); strncpy(modname, kp->name, name_len);
modname[name_len] = '\0'; modname[name_len] = '\0';
count = 0; kernel_add_sysfs_param(modname, kp, name_len+1);
kp_begin = kp;
} }
count++;
}
/* last kernel_params need to be registered as well */
if (count)
kernel_param_sysfs_setup(modname, kp_begin, count,
strlen(modname)+1);
} }
/* module-related sysfs stuff */ /* module-related sysfs stuff */
#define to_module_attr(n) container_of(n, struct module_attribute, attr);
#define to_module_kobject(n) container_of(n, struct module_kobject, kobj);
static ssize_t module_attr_show(struct kobject *kobj, static ssize_t module_attr_show(struct kobject *kobj,
struct attribute *attr, struct attribute *attr,
char *buf) char *buf)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment