1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Helpers for initial module or kernel cmdline parsing
3 Copyright (C) 2001 Rusty Russell.
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>
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 */
22 #define KPARAM_MUTEX(mod) ((mod) ? &(mod)->param_lock : ¶m_lock)
24 #define KPARAM_MUTEX(mod) (¶m_lock)
27 static inline void check_kparam_locked(struct module *mod)
29 BUG_ON(!mutex_is_locked(KPARAM_MUTEX(mod)));
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;
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);
53 spin_lock(&kmalloced_params_lock);
54 list_add(&p->list, &kmalloced_params);
55 spin_unlock(&kmalloced_params_lock);
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) {
73 spin_unlock(&kmalloced_params_lock);
76 static char dash2underscore(char c)
83 bool parameqn(const char *a, const char *b, size_t n)
87 for (i = 0; i < n; i++) {
88 if (dash2underscore(a[i]) != dash2underscore(b[i]))
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",
104 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
108 static int parse_one(char *param,
111 const struct kernel_param *params,
116 int (*handle_unknown)(char *param, char *val,
117 const char *doing, void *arg))
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)
128 /* No one handled NULL, so do it here. */
130 !(params[i].ops->flags & KERNEL_PARAM_OPS_FL_NOARG))
132 pr_debug("handling %s with %p\n", param,
134 kernel_param_lock(params[i].mod);
135 param_check_unsafe(¶ms[i]);
136 err = params[i].ops->set(val, ¶ms[i]);
137 kernel_param_unlock(params[i].mod);
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);
151 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
152 char *parse_args(const char *doing,
154 const struct kernel_param *params,
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);
168 pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
172 int irq_was_disabled;
174 args = next_arg(args, ¶m, &val);
176 if (!val && strcmp(param, "--") == 0)
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",
189 pr_err("%s: Unknown parameter `%s'\n", doing, param);
192 pr_err("%s: `%s' too large for parameter `%s'\n",
193 doing, val ?: "", param);
196 pr_err("%s: `%s' invalid for parameter `%s'\n",
197 doing, val ?: "", param);
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, \
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);
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)
251 strcpy(*(char **)kp->arg, val);
253 *(const char **)kp->arg = val;
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"... */
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)
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);
316 /* Don't let them unset it once it's set! */
317 if (!new_value && orig_value)
321 err = param_set_bool(val, kp);
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)
339 struct kernel_param dummy;
341 dummy.arg = &boolval;
342 ret = param_set_bool(val, &dummy);
344 *(bool *)kp->arg = !boolval;
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;
370 ret = param_set_bool(val, &boolkp);
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,
388 unsigned int min, unsigned int max,
389 void *elem, int elemsize,
390 int (*set)(const char *, const struct kernel_param *kp),
395 struct kernel_param kp;
398 /* Get the name right for errors. */
404 /* We expect a comma-separated list of values. */
409 pr_err("%s: can only take %i arguments\n", name, max);
412 len = strcspn(val, ",");
414 /* nul-terminate and parse */
416 ((char *)val)[len] = '\0';
417 check_kparam_locked(mod);
425 } while (save == ',');
428 pr_err("%s: needs at least %i arguments\n", name, min);
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)
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 */
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);
465 static void param_array_free(void *arg)
468 const struct kparam_array *arr = arg;
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);
491 strcpy(kps->string, val);
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
522 struct attribute_group grp;
523 struct param_attribute attrs[0];
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)
533 struct param_attribute *attribute = to_param_attr(mattr);
535 if (!attribute->param->ops->get)
538 kernel_param_lock(mk->mod);
539 count = attribute->param->ops->get(buf, attribute->param);
540 kernel_param_unlock(mk->mod);
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)
550 struct param_attribute *attribute = to_param_attr(mattr);
552 if (!attribute->param->ops->set)
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);
565 #ifdef CONFIG_MODULES
568 #define __modinit __init
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,
599 struct module_param_attrs *new_mp;
600 struct attribute **new_attrs;
603 /* We don't bother calling this with invisible parameters. */
607 /* First allocation. */
608 mk->mp = kzalloc(sizeof(*mk->mp), GFP_KERNEL);
611 mk->mp->grp.name = "parameters";
612 /* NULL-terminated attribute array. */
613 mk->mp->grp.attrs = kzalloc(sizeof(mk->mp->grp.attrs[0]),
615 /* Caller will cleanup via free_module_param_attrs */
616 if (!mk->mp->grp.attrs)
620 /* Enlarge allocations. */
621 new_mp = krealloc(mk->mp,
623 sizeof(mk->mp->attrs[0]) * (mk->mp->num + 1),
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),
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;
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;
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;
658 #ifdef CONFIG_MODULES
659 static void free_module_param_attrs(struct module_kobject *mk)
662 kfree(mk->mp->grp.attrs);
668 * module_param_sysfs_setup - setup sysfs support for one 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)
683 for (i = 0; i < num_params; i++) {
684 if (kparam[i].perm == 0)
686 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
688 free_module_param_attrs(&mod->mkobj);
697 /* Create the param group. */
698 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
700 free_module_param_attrs(&mod->mkobj);
705 * module_param_sysfs_remove - remove sysfs support for one module
708 * Remove sysfs entries for module parameters and the corresponding
711 void module_param_sysfs_remove(struct module *mod)
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);
722 void destroy_params(const struct kernel_param *params, unsigned num)
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;
737 kobj = kset_find_obj(module_kset, name);
739 mk = to_module_kobject(kobj);
741 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
744 mk->mod = THIS_MODULE;
745 mk->kobj.kset = module_kset;
746 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
748 #ifdef CONFIG_MODULES
750 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
753 kobject_put(&mk->kobj);
754 pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
759 /* So that we hold reference in both cases. */
760 kobject_get(&mk->kobj);
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;
773 mk = locate_module_kobject(name);
777 /* We need to remove old parameters before adding more. */
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);
784 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
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++) {
812 dot = strchr(kp->name, '.');
814 /* This happens for core_param() */
815 strcpy(modname, "kernel");
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;
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);
848 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
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,
862 struct module_attribute *attribute;
863 struct module_kobject *mk;
866 attribute = to_module_attr(attr);
867 mk = to_module_kobject(kobj);
869 if (!attribute->show)
872 ret = attribute->show(attribute, mk, buf);
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;
885 attribute = to_module_attr(attr);
886 mk = to_module_kobject(kobj);
888 if (!attribute->store)
891 ret = attribute->store(attribute, mk, buf, len);
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)
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);
935 printk(KERN_WARNING "%s (%d): error creating kset\n",
939 module_sysfs_initialized = 1;
941 version_sysfs_builtin();
942 param_sysfs_builtin();
946 subsys_initcall(param_sysfs_init);
948 #endif /* CONFIG_SYSFS */