drm/amd/powerplay: Zero initialize some variables
[linux/fpc-iii.git] / kernel / params.c
blobcf448785d0586d72b34366de83362342de4d9838
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Helpers for initial module or kernel cmdline parsing
3 Copyright (C) 2001 Rusty Russell.
5 */
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/errno.h>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/ctype.h>
16 #ifdef CONFIG_SYSFS
17 /* Protects all built-in parameters, modules use their own param_lock */
18 static DEFINE_MUTEX(param_lock);
20 /* Use the module's mutex, or if built-in use the built-in mutex */
21 #ifdef CONFIG_MODULES
22 #define KPARAM_MUTEX(mod) ((mod) ? &(mod)->param_lock : &param_lock)
23 #else
24 #define KPARAM_MUTEX(mod) (&param_lock)
25 #endif
27 static inline void check_kparam_locked(struct module *mod)
29 BUG_ON(!mutex_is_locked(KPARAM_MUTEX(mod)));
31 #else
32 static inline void check_kparam_locked(struct module *mod)
35 #endif /* !CONFIG_SYSFS */
37 /* This just allows us to keep track of which parameters are kmalloced. */
38 struct kmalloced_param {
39 struct list_head list;
40 char val[];
42 static LIST_HEAD(kmalloced_params);
43 static DEFINE_SPINLOCK(kmalloced_params_lock);
45 static void *kmalloc_parameter(unsigned int size)
47 struct kmalloced_param *p;
49 p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
50 if (!p)
51 return NULL;
53 spin_lock(&kmalloced_params_lock);
54 list_add(&p->list, &kmalloced_params);
55 spin_unlock(&kmalloced_params_lock);
57 return p->val;
60 /* Does nothing if parameter wasn't kmalloced above. */
61 static void maybe_kfree_parameter(void *param)
63 struct kmalloced_param *p;
65 spin_lock(&kmalloced_params_lock);
66 list_for_each_entry(p, &kmalloced_params, list) {
67 if (p->val == param) {
68 list_del(&p->list);
69 kfree(p);
70 break;
73 spin_unlock(&kmalloced_params_lock);
76 static char dash2underscore(char c)
78 if (c == '-')
79 return '_';
80 return c;
83 bool parameqn(const char *a, const char *b, size_t n)
85 size_t i;
87 for (i = 0; i < n; i++) {
88 if (dash2underscore(a[i]) != dash2underscore(b[i]))
89 return false;
91 return true;
94 bool parameq(const char *a, const char *b)
96 return parameqn(a, b, strlen(a)+1);
99 static void param_check_unsafe(const struct kernel_param *kp)
101 if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
102 pr_notice("Setting dangerous option %s - tainting kernel\n",
103 kp->name);
104 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
108 static int parse_one(char *param,
109 char *val,
110 const char *doing,
111 const struct kernel_param *params,
112 unsigned num_params,
113 s16 min_level,
114 s16 max_level,
115 void *arg,
116 int (*handle_unknown)(char *param, char *val,
117 const char *doing, void *arg))
119 unsigned int i;
120 int err;
122 /* Find parameter */
123 for (i = 0; i < num_params; i++) {
124 if (parameq(param, params[i].name)) {
125 if (params[i].level < min_level
126 || params[i].level > max_level)
127 return 0;
128 /* No one handled NULL, so do it here. */
129 if (!val &&
130 !(params[i].ops->flags & KERNEL_PARAM_OPS_FL_NOARG))
131 return -EINVAL;
132 pr_debug("handling %s with %p\n", param,
133 params[i].ops->set);
134 kernel_param_lock(params[i].mod);
135 param_check_unsafe(&params[i]);
136 err = params[i].ops->set(val, &params[i]);
137 kernel_param_unlock(params[i].mod);
138 return err;
142 if (handle_unknown) {
143 pr_debug("doing %s: %s='%s'\n", doing, param, val);
144 return handle_unknown(param, val, doing, arg);
147 pr_debug("Unknown argument '%s'\n", param);
148 return -ENOENT;
151 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
152 char *parse_args(const char *doing,
153 char *args,
154 const struct kernel_param *params,
155 unsigned num,
156 s16 min_level,
157 s16 max_level,
158 void *arg,
159 int (*unknown)(char *param, char *val,
160 const char *doing, void *arg))
162 char *param, *val, *err = NULL;
164 /* Chew leading spaces */
165 args = skip_spaces(args);
167 if (*args)
168 pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
170 while (*args) {
171 int ret;
172 int irq_was_disabled;
174 args = next_arg(args, &param, &val);
175 /* Stop at -- */
176 if (!val && strcmp(param, "--") == 0)
177 return err ?: args;
178 irq_was_disabled = irqs_disabled();
179 ret = parse_one(param, val, doing, params, num,
180 min_level, max_level, arg, unknown);
181 if (irq_was_disabled && !irqs_disabled())
182 pr_warn("%s: option '%s' enabled irq's!\n",
183 doing, param);
185 switch (ret) {
186 case 0:
187 continue;
188 case -ENOENT:
189 pr_err("%s: Unknown parameter `%s'\n", doing, param);
190 break;
191 case -ENOSPC:
192 pr_err("%s: `%s' too large for parameter `%s'\n",
193 doing, val ?: "", param);
194 break;
195 default:
196 pr_err("%s: `%s' invalid for parameter `%s'\n",
197 doing, val ?: "", param);
198 break;
201 err = ERR_PTR(ret);
204 return err;
207 /* Lazy bastard, eh? */
208 #define STANDARD_PARAM_DEF(name, type, format, strtolfn) \
209 int param_set_##name(const char *val, const struct kernel_param *kp) \
211 return strtolfn(val, 0, (type *)kp->arg); \
213 int param_get_##name(char *buffer, const struct kernel_param *kp) \
215 return scnprintf(buffer, PAGE_SIZE, format "\n", \
216 *((type *)kp->arg)); \
218 const struct kernel_param_ops param_ops_##name = { \
219 .set = param_set_##name, \
220 .get = param_get_##name, \
221 }; \
222 EXPORT_SYMBOL(param_set_##name); \
223 EXPORT_SYMBOL(param_get_##name); \
224 EXPORT_SYMBOL(param_ops_##name)
227 STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", kstrtou8);
228 STANDARD_PARAM_DEF(short, short, "%hi", kstrtos16);
229 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", kstrtou16);
230 STANDARD_PARAM_DEF(int, int, "%i", kstrtoint);
231 STANDARD_PARAM_DEF(uint, unsigned int, "%u", kstrtouint);
232 STANDARD_PARAM_DEF(long, long, "%li", kstrtol);
233 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", kstrtoul);
234 STANDARD_PARAM_DEF(ullong, unsigned long long, "%llu", kstrtoull);
236 int param_set_charp(const char *val, const struct kernel_param *kp)
238 if (strlen(val) > 1024) {
239 pr_err("%s: string parameter too long\n", kp->name);
240 return -ENOSPC;
243 maybe_kfree_parameter(*(char **)kp->arg);
245 /* This is a hack. We can't kmalloc in early boot, and we
246 * don't need to; this mangled commandline is preserved. */
247 if (slab_is_available()) {
248 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
249 if (!*(char **)kp->arg)
250 return -ENOMEM;
251 strcpy(*(char **)kp->arg, val);
252 } else
253 *(const char **)kp->arg = val;
255 return 0;
257 EXPORT_SYMBOL(param_set_charp);
259 int param_get_charp(char *buffer, const struct kernel_param *kp)
261 return scnprintf(buffer, PAGE_SIZE, "%s\n", *((char **)kp->arg));
263 EXPORT_SYMBOL(param_get_charp);
265 void param_free_charp(void *arg)
267 maybe_kfree_parameter(*((char **)arg));
269 EXPORT_SYMBOL(param_free_charp);
271 const struct kernel_param_ops param_ops_charp = {
272 .set = param_set_charp,
273 .get = param_get_charp,
274 .free = param_free_charp,
276 EXPORT_SYMBOL(param_ops_charp);
278 /* Actually could be a bool or an int, for historical reasons. */
279 int param_set_bool(const char *val, const struct kernel_param *kp)
281 /* No equals means "set"... */
282 if (!val) val = "1";
284 /* One of =[yYnN01] */
285 return strtobool(val, kp->arg);
287 EXPORT_SYMBOL(param_set_bool);
289 int param_get_bool(char *buffer, const struct kernel_param *kp)
291 /* Y and N chosen as being relatively non-coder friendly */
292 return sprintf(buffer, "%c\n", *(bool *)kp->arg ? 'Y' : 'N');
294 EXPORT_SYMBOL(param_get_bool);
296 const struct kernel_param_ops param_ops_bool = {
297 .flags = KERNEL_PARAM_OPS_FL_NOARG,
298 .set = param_set_bool,
299 .get = param_get_bool,
301 EXPORT_SYMBOL(param_ops_bool);
303 int param_set_bool_enable_only(const char *val, const struct kernel_param *kp)
305 int err = 0;
306 bool new_value;
307 bool orig_value = *(bool *)kp->arg;
308 struct kernel_param dummy_kp = *kp;
310 dummy_kp.arg = &new_value;
312 err = param_set_bool(val, &dummy_kp);
313 if (err)
314 return err;
316 /* Don't let them unset it once it's set! */
317 if (!new_value && orig_value)
318 return -EROFS;
320 if (new_value)
321 err = param_set_bool(val, kp);
323 return err;
325 EXPORT_SYMBOL_GPL(param_set_bool_enable_only);
327 const struct kernel_param_ops param_ops_bool_enable_only = {
328 .flags = KERNEL_PARAM_OPS_FL_NOARG,
329 .set = param_set_bool_enable_only,
330 .get = param_get_bool,
332 EXPORT_SYMBOL_GPL(param_ops_bool_enable_only);
334 /* This one must be bool. */
335 int param_set_invbool(const char *val, const struct kernel_param *kp)
337 int ret;
338 bool boolval;
339 struct kernel_param dummy;
341 dummy.arg = &boolval;
342 ret = param_set_bool(val, &dummy);
343 if (ret == 0)
344 *(bool *)kp->arg = !boolval;
345 return ret;
347 EXPORT_SYMBOL(param_set_invbool);
349 int param_get_invbool(char *buffer, const struct kernel_param *kp)
351 return sprintf(buffer, "%c\n", (*(bool *)kp->arg) ? 'N' : 'Y');
353 EXPORT_SYMBOL(param_get_invbool);
355 const struct kernel_param_ops param_ops_invbool = {
356 .set = param_set_invbool,
357 .get = param_get_invbool,
359 EXPORT_SYMBOL(param_ops_invbool);
361 int param_set_bint(const char *val, const struct kernel_param *kp)
363 /* Match bool exactly, by re-using it. */
364 struct kernel_param boolkp = *kp;
365 bool v;
366 int ret;
368 boolkp.arg = &v;
370 ret = param_set_bool(val, &boolkp);
371 if (ret == 0)
372 *(int *)kp->arg = v;
373 return ret;
375 EXPORT_SYMBOL(param_set_bint);
377 const struct kernel_param_ops param_ops_bint = {
378 .flags = KERNEL_PARAM_OPS_FL_NOARG,
379 .set = param_set_bint,
380 .get = param_get_int,
382 EXPORT_SYMBOL(param_ops_bint);
384 /* We break the rule and mangle the string. */
385 static int param_array(struct module *mod,
386 const char *name,
387 const char *val,
388 unsigned int min, unsigned int max,
389 void *elem, int elemsize,
390 int (*set)(const char *, const struct kernel_param *kp),
391 s16 level,
392 unsigned int *num)
394 int ret;
395 struct kernel_param kp;
396 char save;
398 /* Get the name right for errors. */
399 kp.name = name;
400 kp.arg = elem;
401 kp.level = level;
403 *num = 0;
404 /* We expect a comma-separated list of values. */
405 do {
406 int len;
408 if (*num == max) {
409 pr_err("%s: can only take %i arguments\n", name, max);
410 return -EINVAL;
412 len = strcspn(val, ",");
414 /* nul-terminate and parse */
415 save = val[len];
416 ((char *)val)[len] = '\0';
417 check_kparam_locked(mod);
418 ret = set(val, &kp);
420 if (ret != 0)
421 return ret;
422 kp.arg += elemsize;
423 val += len+1;
424 (*num)++;
425 } while (save == ',');
427 if (*num < min) {
428 pr_err("%s: needs at least %i arguments\n", name, min);
429 return -EINVAL;
431 return 0;
434 static int param_array_set(const char *val, const struct kernel_param *kp)
436 const struct kparam_array *arr = kp->arr;
437 unsigned int temp_num;
439 return param_array(kp->mod, kp->name, val, 1, arr->max, arr->elem,
440 arr->elemsize, arr->ops->set, kp->level,
441 arr->num ?: &temp_num);
444 static int param_array_get(char *buffer, const struct kernel_param *kp)
446 int i, off, ret;
447 const struct kparam_array *arr = kp->arr;
448 struct kernel_param p = *kp;
450 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
451 /* Replace \n with comma */
452 if (i)
453 buffer[off - 1] = ',';
454 p.arg = arr->elem + arr->elemsize * i;
455 check_kparam_locked(p.mod);
456 ret = arr->ops->get(buffer + off, &p);
457 if (ret < 0)
458 return ret;
459 off += ret;
461 buffer[off] = '\0';
462 return off;
465 static void param_array_free(void *arg)
467 unsigned int i;
468 const struct kparam_array *arr = arg;
470 if (arr->ops->free)
471 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
472 arr->ops->free(arr->elem + arr->elemsize * i);
475 const struct kernel_param_ops param_array_ops = {
476 .set = param_array_set,
477 .get = param_array_get,
478 .free = param_array_free,
480 EXPORT_SYMBOL(param_array_ops);
482 int param_set_copystring(const char *val, const struct kernel_param *kp)
484 const struct kparam_string *kps = kp->str;
486 if (strlen(val)+1 > kps->maxlen) {
487 pr_err("%s: string doesn't fit in %u chars.\n",
488 kp->name, kps->maxlen-1);
489 return -ENOSPC;
491 strcpy(kps->string, val);
492 return 0;
494 EXPORT_SYMBOL(param_set_copystring);
496 int param_get_string(char *buffer, const struct kernel_param *kp)
498 const struct kparam_string *kps = kp->str;
499 return scnprintf(buffer, PAGE_SIZE, "%s\n", kps->string);
501 EXPORT_SYMBOL(param_get_string);
503 const struct kernel_param_ops param_ops_string = {
504 .set = param_set_copystring,
505 .get = param_get_string,
507 EXPORT_SYMBOL(param_ops_string);
509 /* sysfs output in /sys/modules/XYZ/parameters/ */
510 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
511 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
513 struct param_attribute
515 struct module_attribute mattr;
516 const struct kernel_param *param;
519 struct module_param_attrs
521 unsigned int num;
522 struct attribute_group grp;
523 struct param_attribute attrs[0];
526 #ifdef CONFIG_SYSFS
527 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
529 static ssize_t param_attr_show(struct module_attribute *mattr,
530 struct module_kobject *mk, char *buf)
532 int count;
533 struct param_attribute *attribute = to_param_attr(mattr);
535 if (!attribute->param->ops->get)
536 return -EPERM;
538 kernel_param_lock(mk->mod);
539 count = attribute->param->ops->get(buf, attribute->param);
540 kernel_param_unlock(mk->mod);
541 return count;
544 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
545 static ssize_t param_attr_store(struct module_attribute *mattr,
546 struct module_kobject *mk,
547 const char *buf, size_t len)
549 int err;
550 struct param_attribute *attribute = to_param_attr(mattr);
552 if (!attribute->param->ops->set)
553 return -EPERM;
555 kernel_param_lock(mk->mod);
556 param_check_unsafe(attribute->param);
557 err = attribute->param->ops->set(buf, attribute->param);
558 kernel_param_unlock(mk->mod);
559 if (!err)
560 return len;
561 return err;
563 #endif
565 #ifdef CONFIG_MODULES
566 #define __modinit
567 #else
568 #define __modinit __init
569 #endif
571 #ifdef CONFIG_SYSFS
572 void kernel_param_lock(struct module *mod)
574 mutex_lock(KPARAM_MUTEX(mod));
577 void kernel_param_unlock(struct module *mod)
579 mutex_unlock(KPARAM_MUTEX(mod));
582 EXPORT_SYMBOL(kernel_param_lock);
583 EXPORT_SYMBOL(kernel_param_unlock);
586 * add_sysfs_param - add a parameter to sysfs
587 * @mk: struct module_kobject
588 * @kp: the actual parameter definition to add to sysfs
589 * @name: name of parameter
591 * Create a kobject if for a (per-module) parameter if mp NULL, and
592 * create file in sysfs. Returns an error on out of memory. Always cleans up
593 * if there's an error.
595 static __modinit int add_sysfs_param(struct module_kobject *mk,
596 const struct kernel_param *kp,
597 const char *name)
599 struct module_param_attrs *new_mp;
600 struct attribute **new_attrs;
601 unsigned int i;
603 /* We don't bother calling this with invisible parameters. */
604 BUG_ON(!kp->perm);
606 if (!mk->mp) {
607 /* First allocation. */
608 mk->mp = kzalloc(sizeof(*mk->mp), GFP_KERNEL);
609 if (!mk->mp)
610 return -ENOMEM;
611 mk->mp->grp.name = "parameters";
612 /* NULL-terminated attribute array. */
613 mk->mp->grp.attrs = kzalloc(sizeof(mk->mp->grp.attrs[0]),
614 GFP_KERNEL);
615 /* Caller will cleanup via free_module_param_attrs */
616 if (!mk->mp->grp.attrs)
617 return -ENOMEM;
620 /* Enlarge allocations. */
621 new_mp = krealloc(mk->mp,
622 sizeof(*mk->mp) +
623 sizeof(mk->mp->attrs[0]) * (mk->mp->num + 1),
624 GFP_KERNEL);
625 if (!new_mp)
626 return -ENOMEM;
627 mk->mp = new_mp;
629 /* Extra pointer for NULL terminator */
630 new_attrs = krealloc(mk->mp->grp.attrs,
631 sizeof(mk->mp->grp.attrs[0]) * (mk->mp->num + 2),
632 GFP_KERNEL);
633 if (!new_attrs)
634 return -ENOMEM;
635 mk->mp->grp.attrs = new_attrs;
637 /* Tack new one on the end. */
638 memset(&mk->mp->attrs[mk->mp->num], 0, sizeof(mk->mp->attrs[0]));
639 sysfs_attr_init(&mk->mp->attrs[mk->mp->num].mattr.attr);
640 mk->mp->attrs[mk->mp->num].param = kp;
641 mk->mp->attrs[mk->mp->num].mattr.show = param_attr_show;
642 /* Do not allow runtime DAC changes to make param writable. */
643 if ((kp->perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
644 mk->mp->attrs[mk->mp->num].mattr.store = param_attr_store;
645 else
646 mk->mp->attrs[mk->mp->num].mattr.store = NULL;
647 mk->mp->attrs[mk->mp->num].mattr.attr.name = (char *)name;
648 mk->mp->attrs[mk->mp->num].mattr.attr.mode = kp->perm;
649 mk->mp->num++;
651 /* Fix up all the pointers, since krealloc can move us */
652 for (i = 0; i < mk->mp->num; i++)
653 mk->mp->grp.attrs[i] = &mk->mp->attrs[i].mattr.attr;
654 mk->mp->grp.attrs[mk->mp->num] = NULL;
655 return 0;
658 #ifdef CONFIG_MODULES
659 static void free_module_param_attrs(struct module_kobject *mk)
661 if (mk->mp)
662 kfree(mk->mp->grp.attrs);
663 kfree(mk->mp);
664 mk->mp = NULL;
668 * module_param_sysfs_setup - setup sysfs support for one module
669 * @mod: module
670 * @kparam: module parameters (array)
671 * @num_params: number of module parameters
673 * Adds sysfs entries for module parameters under
674 * /sys/module/[mod->name]/parameters/
676 int module_param_sysfs_setup(struct module *mod,
677 const struct kernel_param *kparam,
678 unsigned int num_params)
680 int i, err;
681 bool params = false;
683 for (i = 0; i < num_params; i++) {
684 if (kparam[i].perm == 0)
685 continue;
686 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
687 if (err) {
688 free_module_param_attrs(&mod->mkobj);
689 return err;
691 params = true;
694 if (!params)
695 return 0;
697 /* Create the param group. */
698 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
699 if (err)
700 free_module_param_attrs(&mod->mkobj);
701 return err;
705 * module_param_sysfs_remove - remove sysfs support for one module
706 * @mod: module
708 * Remove sysfs entries for module parameters and the corresponding
709 * kobject.
711 void module_param_sysfs_remove(struct module *mod)
713 if (mod->mkobj.mp) {
714 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
715 /* We are positive that no one is using any param
716 * attrs at this point. Deallocate immediately. */
717 free_module_param_attrs(&mod->mkobj);
720 #endif
722 void destroy_params(const struct kernel_param *params, unsigned num)
724 unsigned int i;
726 for (i = 0; i < num; i++)
727 if (params[i].ops->free)
728 params[i].ops->free(params[i].arg);
731 static struct module_kobject * __init locate_module_kobject(const char *name)
733 struct module_kobject *mk;
734 struct kobject *kobj;
735 int err;
737 kobj = kset_find_obj(module_kset, name);
738 if (kobj) {
739 mk = to_module_kobject(kobj);
740 } else {
741 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
742 BUG_ON(!mk);
744 mk->mod = THIS_MODULE;
745 mk->kobj.kset = module_kset;
746 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
747 "%s", name);
748 #ifdef CONFIG_MODULES
749 if (!err)
750 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
751 #endif
752 if (err) {
753 kobject_put(&mk->kobj);
754 pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
755 name, err);
756 return NULL;
759 /* So that we hold reference in both cases. */
760 kobject_get(&mk->kobj);
763 return mk;
766 static void __init kernel_add_sysfs_param(const char *name,
767 const struct kernel_param *kparam,
768 unsigned int name_skip)
770 struct module_kobject *mk;
771 int err;
773 mk = locate_module_kobject(name);
774 if (!mk)
775 return;
777 /* We need to remove old parameters before adding more. */
778 if (mk->mp)
779 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
781 /* These should not fail at boot. */
782 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
783 BUG_ON(err);
784 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
785 BUG_ON(err);
786 kobject_uevent(&mk->kobj, KOBJ_ADD);
787 kobject_put(&mk->kobj);
791 * param_sysfs_builtin - add sysfs parameters for built-in modules
793 * Add module_parameters to sysfs for "modules" built into the kernel.
795 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
796 * "parameter" name is stored behind a dot in kernel_param->name. So,
797 * extract the "module" name for all built-in kernel_param-eters,
798 * and for all who have the same, call kernel_add_sysfs_param.
800 static void __init param_sysfs_builtin(void)
802 const struct kernel_param *kp;
803 unsigned int name_len;
804 char modname[MODULE_NAME_LEN];
806 for (kp = __start___param; kp < __stop___param; kp++) {
807 char *dot;
809 if (kp->perm == 0)
810 continue;
812 dot = strchr(kp->name, '.');
813 if (!dot) {
814 /* This happens for core_param() */
815 strcpy(modname, "kernel");
816 name_len = 0;
817 } else {
818 name_len = dot - kp->name + 1;
819 strlcpy(modname, kp->name, name_len);
821 kernel_add_sysfs_param(modname, kp, name_len);
825 ssize_t __modver_version_show(struct module_attribute *mattr,
826 struct module_kobject *mk, char *buf)
828 struct module_version_attribute *vattr =
829 container_of(mattr, struct module_version_attribute, mattr);
831 return scnprintf(buf, PAGE_SIZE, "%s\n", vattr->version);
834 extern const struct module_version_attribute *__start___modver[];
835 extern const struct module_version_attribute *__stop___modver[];
837 static void __init version_sysfs_builtin(void)
839 const struct module_version_attribute **p;
840 struct module_kobject *mk;
841 int err;
843 for (p = __start___modver; p < __stop___modver; p++) {
844 const struct module_version_attribute *vattr = *p;
846 mk = locate_module_kobject(vattr->module_name);
847 if (mk) {
848 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
849 WARN_ON_ONCE(err);
850 kobject_uevent(&mk->kobj, KOBJ_ADD);
851 kobject_put(&mk->kobj);
856 /* module-related sysfs stuff */
858 static ssize_t module_attr_show(struct kobject *kobj,
859 struct attribute *attr,
860 char *buf)
862 struct module_attribute *attribute;
863 struct module_kobject *mk;
864 int ret;
866 attribute = to_module_attr(attr);
867 mk = to_module_kobject(kobj);
869 if (!attribute->show)
870 return -EIO;
872 ret = attribute->show(attribute, mk, buf);
874 return ret;
877 static ssize_t module_attr_store(struct kobject *kobj,
878 struct attribute *attr,
879 const char *buf, size_t len)
881 struct module_attribute *attribute;
882 struct module_kobject *mk;
883 int ret;
885 attribute = to_module_attr(attr);
886 mk = to_module_kobject(kobj);
888 if (!attribute->store)
889 return -EIO;
891 ret = attribute->store(attribute, mk, buf, len);
893 return ret;
896 static const struct sysfs_ops module_sysfs_ops = {
897 .show = module_attr_show,
898 .store = module_attr_store,
901 static int uevent_filter(struct kset *kset, struct kobject *kobj)
903 struct kobj_type *ktype = get_ktype(kobj);
905 if (ktype == &module_ktype)
906 return 1;
907 return 0;
910 static const struct kset_uevent_ops module_uevent_ops = {
911 .filter = uevent_filter,
914 struct kset *module_kset;
915 int module_sysfs_initialized;
917 static void module_kobj_release(struct kobject *kobj)
919 struct module_kobject *mk = to_module_kobject(kobj);
920 complete(mk->kobj_completion);
923 struct kobj_type module_ktype = {
924 .release = module_kobj_release,
925 .sysfs_ops = &module_sysfs_ops,
929 * param_sysfs_init - wrapper for built-in params support
931 static int __init param_sysfs_init(void)
933 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
934 if (!module_kset) {
935 printk(KERN_WARNING "%s (%d): error creating kset\n",
936 __FILE__, __LINE__);
937 return -ENOMEM;
939 module_sysfs_initialized = 1;
941 version_sysfs_builtin();
942 param_sysfs_builtin();
944 return 0;
946 subsys_initcall(param_sysfs_init);
948 #endif /* CONFIG_SYSFS */