996 lines · 23.7 KB · C
RawDownload
	if (arr->ops->free)		for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)			arr->ops->free(arr->elem + arr->elemsize * i);} const struct kernel_param_ops param_array_ops = {	.set = param_array_set,	.get = param_array_get,	.free = param_array_free,};EXPORT_SYMBOL(param_array_ops); int param_set_copystring(const char *val, const struct kernel_param *kp){	const struct kparam_string *kps = kp->str;	const size_t len = strnlen(val, kps->maxlen); 	if (len == kps->maxlen) {		pr_err("%s: string doesn't fit in %u chars.\n",		       kp->name, kps->maxlen-1);		return -ENOSPC;	}	memcpy(kps->string, val, len + 1);	return 0;}EXPORT_SYMBOL(param_set_copystring); int param_get_string(char *buffer, const struct kernel_param *kp){	const struct kparam_string *kps = kp->str;	return scnprintf(buffer, PAGE_SIZE, "%s\n", kps->string);}EXPORT_SYMBOL(param_get_string); const struct kernel_param_ops param_ops_string = {	.set = param_set_copystring,	.get = param_get_string,};EXPORT_SYMBOL(param_ops_string); /* sysfs output in /sys/modules/XYZ/parameters/ */#define to_module_attr(n) container_of_const(n, struct module_attribute, attr)#define to_module_kobject(n) container_of(n, struct module_kobject, kobj) struct param_attribute{	struct module_attribute mattr;	const struct kernel_param *param;}; struct module_param_attrs{	unsigned int num;	struct attribute_group grp;	struct param_attribute attrs[] __counted_by(num);}; #ifdef CONFIG_SYSFS#define to_param_attr(n) container_of_const(n, struct param_attribute, mattr) static ssize_t param_attr_show(const struct module_attribute *mattr,			       struct module_kobject *mk, char *buf){	int count;	const struct param_attribute *attribute = to_param_attr(mattr); 	if (!attribute->param->ops->get)		return -EPERM; 	kernel_param_lock(mk->mod);	count = attribute->param->ops->get(buf, attribute->param);	kernel_param_unlock(mk->mod);	return count;} /* sysfs always hands a nul-terminated string in buf.  We rely on that. */static ssize_t param_attr_store(const struct module_attribute *mattr,				struct module_kobject *mk,				const char *buf, size_t len){ 	int err;	const struct param_attribute *attribute = to_param_attr(mattr); 	if (!attribute->param->ops->set)		return -EPERM; 	kernel_param_lock(mk->mod);	if (param_check_unsafe(attribute->param))		err = attribute->param->ops->set(buf, attribute->param);	else		err = -EPERM;	kernel_param_unlock(mk->mod);	if (!err)		return len;	return err;}#endif #ifdef CONFIG_SYSFSvoid kernel_param_lock(struct module *mod){	mutex_lock(KPARAM_MUTEX(mod));} void kernel_param_unlock(struct module *mod){	mutex_unlock(KPARAM_MUTEX(mod));} EXPORT_SYMBOL(kernel_param_lock);EXPORT_SYMBOL(kernel_param_unlock); /* * add_sysfs_param - add a parameter to sysfs * @mk: struct module_kobject * @kp: the actual parameter definition to add to sysfs * @name: name of parameter * * Create a kobject if for a (per-module) parameter if mp NULL, and * create file in sysfs.  Returns an error on out of memory.  Always cleans up * if there's an error. */static __init_or_module int add_sysfs_param(struct module_kobject *mk,					    const struct kernel_param *kp,					    const char *name){	struct module_param_attrs *new_mp;	struct attribute **new_attrs;	unsigned int i; 	/* We don't bother calling this with invisible parameters. */	BUG_ON(!kp->perm); 	if (!mk->mp) {		/* First allocation. */		mk->mp = kzalloc_obj(*mk->mp);		if (!mk->mp)			return -ENOMEM;		mk->mp->grp.name = "parameters";		/* NULL-terminated attribute array. */		mk->mp->grp.attrs = kzalloc_obj(mk->mp->grp.attrs[0]);		/* Caller will cleanup via free_module_param_attrs */		if (!mk->mp->grp.attrs)			return -ENOMEM;	} 	/* Enlarge allocations. */	new_mp = krealloc(mk->mp, struct_size(mk->mp, attrs, mk->mp->num + 1),			  GFP_KERNEL);	if (!new_mp)		return -ENOMEM;	mk->mp = new_mp;	mk->mp->num++; 	/* Extra pointer for NULL terminator */	new_attrs = krealloc_array(mk->mp->grp.attrs, mk->mp->num + 1,				   sizeof(mk->mp->grp.attrs[0]), GFP_KERNEL);	if (!new_attrs)		return -ENOMEM;	mk->mp->grp.attrs = new_attrs; 	/* Tack new one on the end. */	memset(&mk->mp->attrs[mk->mp->num - 1], 0, sizeof(mk->mp->attrs[0]));	sysfs_attr_init(&mk->mp->attrs[mk->mp->num - 1].mattr.attr);	mk->mp->attrs[mk->mp->num - 1].param = kp;	mk->mp->attrs[mk->mp->num - 1].mattr.show = param_attr_show;	/* Do not allow runtime DAC changes to make param writable. */	if ((kp->perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)		mk->mp->attrs[mk->mp->num - 1].mattr.store = param_attr_store;	else		mk->mp->attrs[mk->mp->num - 1].mattr.store = NULL;	mk->mp->attrs[mk->mp->num - 1].mattr.attr.name = (char *)name;	mk->mp->attrs[mk->mp->num - 1].mattr.attr.mode = kp->perm; 	/* Fix up all the pointers, since krealloc can move us */	for (i = 0; i < mk->mp->num; i++)		mk->mp->grp.attrs[i] = &mk->mp->attrs[i].mattr.attr;	mk->mp->grp.attrs[mk->mp->num] = NULL;	return 0;} #ifdef CONFIG_MODULESstatic void free_module_param_attrs(struct module_kobject *mk){	if (mk->mp)		kfree(mk->mp->grp.attrs);	kfree(mk->mp);	mk->mp = NULL;} /* * module_param_sysfs_setup - setup sysfs support for one module * @mod: module * @kparam: module parameters (array) * @num_params: number of module parameters * * Adds sysfs entries for module parameters under * /sys/module/[mod->name]/parameters/ */int module_param_sysfs_setup(struct module *mod,			     const struct kernel_param *kparam,			     unsigned int num_params){	int i, err;	bool params = false; 	for (i = 0; i < num_params; i++) {		if (kparam[i].perm == 0)			continue;		err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);		if (err) {			free_module_param_attrs(&mod->mkobj);			return err;		}		params = true;	} 	if (!params)		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;} /* * module_param_sysfs_remove - remove sysfs support for one module * @mod: module * * Remove sysfs entries for module parameters and the corresponding * kobject. */void module_param_sysfs_remove(struct module *mod){	if (mod->mkobj.mp) {		sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);		/*		 * We are positive that no one is using any param		 * attrs at this point. Deallocate immediately.		 */		free_module_param_attrs(&mod->mkobj);	}}#endif struct module_kobject * __init_or_modulelookup_or_create_module_kobject(const char *name){	struct module_kobject *mk;	struct kobject *kobj;	int err; 	kobj = kset_find_obj(module_kset, name);	if (kobj)		return to_module_kobject(kobj); 	mk = kzalloc_obj(struct module_kobject);	if (!mk)		return NULL; 	mk->mod = THIS_MODULE;	mk->kobj.kset = module_kset;	err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL, "%s", name);	if (IS_ENABLED(CONFIG_MODULES) && !err)		err = sysfs_create_file(&mk->kobj, &module_uevent.attr);	if (err) {		kobject_put(&mk->kobj);		pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",			name, err);		return NULL;	} 	/* So that we hold reference in both cases. */	kobject_get(&mk->kobj); 	return mk;} static void __init kernel_add_sysfs_param(const char *name,					  const struct kernel_param *kparam,					  unsigned int name_skip){	struct module_kobject *mk;	int err; 	mk = lookup_or_create_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);	err = sysfs_create_group(&mk->kobj, &mk->mp->grp);	BUG_ON(err);	kobject_uevent(&mk->kobj, KOBJ_ADD);	kobject_put(&mk->kobj);} /* * param_sysfs_builtin - add sysfs parameters for built-in modules * * Add module_parameters to sysfs for "modules" built into the kernel. * * The "module" name (KBUILD_MODNAME) is stored before a dot, the * "parameter" name is stored behind a dot in kernel_param->name. So, * extract the "module" name for all built-in kernel_param-eters, * and for all who have the same, call kernel_add_sysfs_param. */static void __init param_sysfs_builtin(void){	const struct kernel_param *kp;	unsigned int name_len;	char modname[MODULE_NAME_LEN]; 	for (kp = __start___param; kp < __stop___param; kp++) {		char *dot; 		if (kp->perm == 0)			continue; 		dot = strchr(kp->name, '.');		if (!dot) {			/* This happens for core_param() */			strscpy(modname, "kernel");			name_len = 0;		} else {			name_len = dot - kp->name + 1;			strscpy(modname, kp->name, name_len);		}		kernel_add_sysfs_param(modname, kp, name_len);	}} ssize_t __modver_version_show(const struct module_attribute *mattr,			      struct module_kobject *mk, char *buf){	const struct module_version_attribute *vattr =		container_of_const(mattr, struct module_version_attribute, mattr); 	return scnprintf(buf, PAGE_SIZE, "%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 *vattr;	struct module_kobject *mk;	int err; 	for (vattr = __start___modver; vattr < __stop___modver; vattr++) {		mk = lookup_or_create_module_kobject(vattr->module_name);		if (mk) {			err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);			WARN_ON_ONCE(err);			kobject_uevent(&mk->kobj, KOBJ_ADD);			kobject_put(&mk->kobj);		}	}} /* module-related sysfs stuff */ static ssize_t module_attr_show(struct kobject *kobj,				struct attribute *attr,				char *buf){	const struct module_attribute *attribute;	struct module_kobject *mk;	int ret; 	attribute = to_module_attr(attr);	mk = to_module_kobject(kobj); 	if (!attribute->show)		return -EIO; 	ret = attribute->show(attribute, mk, buf); 	return ret;} static ssize_t module_attr_store(struct kobject *kobj,				struct attribute *attr,				const char *buf, size_t len){	const struct module_attribute *attribute;	struct module_kobject *mk;	int ret; 	attribute = to_module_attr(attr);	mk = to_module_kobject(kobj); 	if (!attribute->store)		return -EIO; 	ret = attribute->store(attribute, mk, buf, len); 	return ret;} static const struct sysfs_ops module_sysfs_ops = {	.show = module_attr_show,	.store = module_attr_store,}; static int uevent_filter(const struct kobject *kobj){	const struct kobj_type *ktype = get_ktype(kobj); 	if (ktype == &module_ktype)		return 1;	return 0;} static const struct kset_uevent_ops module_uevent_ops = {	.filter = uevent_filter,}; struct kset *module_kset; static void module_kobj_release(struct kobject *kobj){	struct module_kobject *mk = to_module_kobject(kobj); 	if (mk->kobj_completion)		complete(mk->kobj_completion);} const struct kobj_type module_ktype = {	.release   =	module_kobj_release,	.sysfs_ops =	&module_sysfs_ops,}; /* * param_sysfs_init - create "module" kset * * This must be done before any driver registration so that when a driver comes * from a built-in module, the driver core can add the module under /sys/module * and create the associated driver symlinks. */static int __init param_sysfs_init(void){	module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);	if (!module_kset) {		printk(KERN_WARNING "%s (%d): error creating kset\n",			__FILE__, __LINE__);		return -ENOMEM;	} 	return 0;}pure_initcall(param_sysfs_init); /* * param_sysfs_builtin_init - add sysfs version and parameter * attributes for built-in modules */static int __init param_sysfs_builtin_init(void){	if (!module_kset)		return -ENOMEM; 	version_sysfs_builtin();	param_sysfs_builtin(); 	return 0;}late_initcall(param_sysfs_builtin_init); #endif /* CONFIG_SYSFS */ #ifdef CONFIG_MODULES /* * module_destroy_params - free all parameters for one module * @params: module parameters (array) * @num: number of module parameters */void module_destroy_params(const struct kernel_param *params, unsigned int num){	unsigned int i; 	for (i = 0; i < num; i++)		if (params[i].ops->free)			params[i].ops->free(params[i].arg);} #endif /* CONFIG_MODULES */

Lines 501–996 of 996. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t