1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Helpers for initial module or kernel cmdline parsing
4 * Copyright (C) 2001 Rusty Russell.
5 */
6#include <linux/ctype.h>
7#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/errno.h>
10#include <linux/kernel.h>
11#include <linux/kstrtox.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/overflow.h>
15#include <linux/security.h>
16#include <linux/slab.h>
17#include <linux/string.h>
18
19#ifdef CONFIG_SYSFS
20/* Protects all built-in parameters, modules use their own param_lock */
21static DEFINE_MUTEX(param_lock);
22
23/* Use the module's mutex, or if built-in use the built-in mutex */
24#ifdef CONFIG_MODULES
25#define KPARAM_MUTEX(mod) ((mod) ? &(mod)->param_lock : &param_lock)
26#else
27#define KPARAM_MUTEX(mod) (&param_lock)
28#endif
29
30static inline void check_kparam_locked(struct module *mod)
31{
32 BUG_ON(!mutex_is_locked(KPARAM_MUTEX(mod)));
33}
34#else
35static inline void check_kparam_locked(struct module *mod)
36{
37}
38#endif /* !CONFIG_SYSFS */
39
40/* This just allows us to keep track of which parameters are kmalloced. */
41struct kmalloced_param {
42 struct list_head list;
43 char val[];
44};
45static LIST_HEAD(kmalloced_params);
46static DEFINE_SPINLOCK(kmalloced_params_lock);
47
48static void *kmalloc_parameter(unsigned int size)
49{
50 struct kmalloced_param *p;
51
52 p = kmalloc(size_add(sizeof(*p), size), GFP_KERNEL);
53 if (!p)
54 return NULL;
55
56 spin_lock(lock: &kmalloced_params_lock);
57 list_add(new: &p->list, head: &kmalloced_params);
58 spin_unlock(lock: &kmalloced_params_lock);
59
60 return p->val;
61}
62
63/* Does nothing if parameter wasn't kmalloced above. */
64static void maybe_kfree_parameter(void *param)
65{
66 struct kmalloced_param *p;
67
68 spin_lock(lock: &kmalloced_params_lock);
69 list_for_each_entry(p, &kmalloced_params, list) {
70 if (p->val == param) {
71 list_del(entry: &p->list);
72 kfree(objp: p);
73 break;
74 }
75 }
76 spin_unlock(lock: &kmalloced_params_lock);
77}
78
79static char dash2underscore(char c)
80{
81 if (c == '-')
82 return '_';
83 return c;
84}
85
86bool parameqn(const char *a, const char *b, size_t n)
87{
88 size_t i;
89
90 for (i = 0; i < n; i++) {
91 if (dash2underscore(c: a[i]) != dash2underscore(c: b[i]))
92 return false;
93 }
94 return true;
95}
96
97bool parameq(const char *a, const char *b)
98{
99 return parameqn(a, b, n: strlen(a)+1);
100}
101
102static bool param_check_unsafe(const struct kernel_param *kp)
103{
104 if (kp->flags & KERNEL_PARAM_FL_HWPARAM &&
105 security_locked_down(what: LOCKDOWN_MODULE_PARAMETERS))
106 return false;
107
108 if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
109 pr_notice("Setting dangerous option %s - tainting kernel\n",
110 kp->name);
111 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
112 }
113
114 return true;
115}
116
117static int parse_one(char *param,
118 char *val,
119 const char *doing,
120 const struct kernel_param *params,
121 unsigned num_params,
122 s16 min_level,
123 s16 max_level,
124 void *arg, parse_unknown_fn handle_unknown)
125{
126 unsigned int i;
127 int err;
128
129 /* Find parameter */
130 for (i = 0; i < num_params; i++) {
131 if (parameq(a: param, b: params[i].name)) {
132 if (params[i].level < min_level
133 || params[i].level > max_level)
134 return 0;
135 /* No one handled NULL, so do it here. */
136 if (!val &&
137 !(params[i].ops->flags & KERNEL_PARAM_OPS_FL_NOARG))
138 return -EINVAL;
139 pr_debug("handling %s with %p\n", param,
140 params[i].ops->set);
141 kernel_param_lock(mod: params[i].mod);
142 if (param_check_unsafe(kp: &params[i]))
143 err = params[i].ops->set(val, &params[i]);
144 else
145 err = -EPERM;
146 kernel_param_unlock(mod: params[i].mod);
147 return err;
148 }
149 }
150
151 if (handle_unknown) {
152 pr_debug("doing %s: %s='%s'\n", doing, param, val);
153 return handle_unknown(param, val, doing, arg);
154 }
155
156 pr_debug("Unknown argument '%s'\n", param);
157 return -ENOENT;
158}
159
160/* Args looks like "foo=bar,bar2 baz=fuz wiz". */
161char *parse_args(const char *doing,
162 char *args,
163 const struct kernel_param *params,
164 unsigned num,
165 s16 min_level,
166 s16 max_level,
167 void *arg, parse_unknown_fn unknown)
168{
169 char *param, *val, *err = NULL;
170
171 /* Chew leading spaces */
172 args = skip_spaces(args);
173
174 if (*args)
175 pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
176
177 while (*args) {
178 int ret;
179 int irq_was_disabled;
180
181 args = next_arg(args, param: &param, val: &val);
182 /* Stop at -- */
183 if (!val && strcmp(param, "--") == 0)
184 return err ?: args;
185 irq_was_disabled = irqs_disabled();
186 ret = parse_one(param, val, doing, params, num_params: num,
187 min_level, max_level, arg, handle_unknown: unknown);
188 if (irq_was_disabled && !irqs_disabled())
189 pr_warn("%s: option '%s' enabled irq's!\n",
190 doing, param);
191
192 switch (ret) {
193 case 0:
194 continue;
195 case -ENOENT:
196 pr_err("%s: Unknown parameter `%s'\n", doing, param);
197 break;
198 case -ENOSPC:
199 pr_err("%s: `%s' too large for parameter `%s'\n",
200 doing, val ?: "", param);
201 break;
202 default:
203 pr_err("%s: `%s' invalid for parameter `%s'\n",
204 doing, val ?: "", param);
205 break;
206 }
207
208 err = ERR_PTR(error: ret);
209 }
210
211 return err;
212}
213
214/* Lazy bastard, eh? */
215#define STANDARD_PARAM_DEF(name, type, format, strtolfn) \
216 int param_set_##name(const char *val, const struct kernel_param *kp) \
217 { \
218 return strtolfn(val, 0, (type *)kp->arg); \
219 } \
220 int param_get_##name(char *buffer, const struct kernel_param *kp) \
221 { \
222 return scnprintf(buffer, PAGE_SIZE, format "\n", \
223 *((type *)kp->arg)); \
224 } \
225 const struct kernel_param_ops param_ops_##name = { \
226 .set = param_set_##name, \
227 .get = param_get_##name, \
228 }; \
229 EXPORT_SYMBOL(param_set_##name); \
230 EXPORT_SYMBOL(param_get_##name); \
231 EXPORT_SYMBOL(param_ops_##name)
232
233
234STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", kstrtou8);
235STANDARD_PARAM_DEF(short, short, "%hi", kstrtos16);
236STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", kstrtou16);
237STANDARD_PARAM_DEF(int, int, "%i", kstrtoint);
238STANDARD_PARAM_DEF(uint, unsigned int, "%u", kstrtouint);
239STANDARD_PARAM_DEF(long, long, "%li", kstrtol);
240STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", kstrtoul);
241STANDARD_PARAM_DEF(ullong, unsigned long long, "%llu", kstrtoull);
242STANDARD_PARAM_DEF(hexint, unsigned int, "%#08x", kstrtouint);
243
244int param_set_uint_minmax(const char *val, const struct kernel_param *kp,
245 unsigned int min, unsigned int max)
246{
247 unsigned int num;
248 int ret;
249
250 if (!val)
251 return -EINVAL;
252 ret = kstrtouint(s: val, base: 0, res: &num);
253 if (ret)
254 return ret;
255 if (num < min || num > max)
256 return -EINVAL;
257 *((unsigned int *)kp->arg) = num;
258 return 0;
259}
260EXPORT_SYMBOL_GPL(param_set_uint_minmax);
261
262int param_set_charp(const char *val, const struct kernel_param *kp)
263{
264 size_t len, maxlen = 1024;
265
266 len = strnlen(val, maxlen + 1);
267 if (len == maxlen + 1) {
268 pr_err("%s: string parameter too long\n", kp->name);
269 return -ENOSPC;
270 }
271
272 maybe_kfree_parameter(param: *(char **)kp->arg);
273
274 /*
275 * This is a hack. We can't kmalloc() in early boot, and we
276 * don't need to; this mangled commandline is preserved.
277 */
278 if (slab_is_available()) {
279 *(char **)kp->arg = kmalloc_parameter(size: len + 1);
280 if (!*(char **)kp->arg)
281 return -ENOMEM;
282 strcpy(*(char **)kp->arg, val);
283 } else
284 *(const char **)kp->arg = val;
285
286 return 0;
287}
288EXPORT_SYMBOL(param_set_charp);
289
290int param_get_charp(char *buffer, const struct kernel_param *kp)
291{
292 return scnprintf(buf: buffer, PAGE_SIZE, fmt: "%s\n", *((char **)kp->arg));
293}
294EXPORT_SYMBOL(param_get_charp);
295
296void param_free_charp(void *arg)
297{
298 maybe_kfree_parameter(param: *((char **)arg));
299}
300EXPORT_SYMBOL(param_free_charp);
301
302const struct kernel_param_ops param_ops_charp = {
303 .set = param_set_charp,
304 .get = param_get_charp,
305 .free = param_free_charp,
306};
307EXPORT_SYMBOL(param_ops_charp);
308
309/* Actually could be a bool or an int, for historical reasons. */
310int param_set_bool(const char *val, const struct kernel_param *kp)
311{
312 /* No equals means "set"... */
313 if (!val) val = "1";
314
315 /* One of =[yYnN01] */
316 return kstrtobool(s: val, res: kp->arg);
317}
318EXPORT_SYMBOL(param_set_bool);
319
320int param_get_bool(char *buffer, const struct kernel_param *kp)
321{
322 /* Y and N chosen as being relatively non-coder friendly */
323 return sprintf(buf: buffer, fmt: "%c\n", *(bool *)kp->arg ? 'Y' : 'N');
324}
325EXPORT_SYMBOL(param_get_bool);
326
327const struct kernel_param_ops param_ops_bool = {
328 .flags = KERNEL_PARAM_OPS_FL_NOARG,
329 .set = param_set_bool,
330 .get = param_get_bool,
331};
332EXPORT_SYMBOL(param_ops_bool);
333
334int param_set_bool_enable_only(const char *val, const struct kernel_param *kp)
335{
336 int err;
337 bool new_value;
338 bool orig_value = *(bool *)kp->arg;
339 struct kernel_param dummy_kp = *kp;
340
341 dummy_kp.arg = &new_value;
342
343 err = param_set_bool(val, &dummy_kp);
344 if (err)
345 return err;
346
347 /* Don't let them unset it once it's set! */
348 if (!new_value && orig_value)
349 return -EROFS;
350
351 if (new_value)
352 err = param_set_bool(val, kp);
353
354 return err;
355}
356EXPORT_SYMBOL_GPL(param_set_bool_enable_only);
357
358const struct kernel_param_ops param_ops_bool_enable_only = {
359 .flags = KERNEL_PARAM_OPS_FL_NOARG,
360 .set = param_set_bool_enable_only,
361 .get = param_get_bool,
362};
363EXPORT_SYMBOL_GPL(param_ops_bool_enable_only);
364
365/* This one must be bool. */
366int param_set_invbool(const char *val, const struct kernel_param *kp)
367{
368 int ret;
369 bool boolval;
370 struct kernel_param dummy;
371
372 dummy.arg = &boolval;
373 ret = param_set_bool(val, &dummy);
374 if (ret == 0)
375 *(bool *)kp->arg = !boolval;
376 return ret;
377}
378EXPORT_SYMBOL(param_set_invbool);
379
380int param_get_invbool(char *buffer, const struct kernel_param *kp)
381{
382 return sprintf(buf: buffer, fmt: "%c\n", (*(bool *)kp->arg) ? 'N' : 'Y');
383}
384EXPORT_SYMBOL(param_get_invbool);
385
386const struct kernel_param_ops param_ops_invbool = {
387 .set = param_set_invbool,
388 .get = param_get_invbool,
389};
390EXPORT_SYMBOL(param_ops_invbool);
391
392int param_set_bint(const char *val, const struct kernel_param *kp)
393{
394 /* Match bool exactly, by re-using it. */
395 struct kernel_param boolkp = *kp;
396 bool v;
397 int ret;
398
399 boolkp.arg = &v;
400
401 ret = param_set_bool(val, &boolkp);
402 if (ret == 0)
403 *(int *)kp->arg = v;
404 return ret;
405}
406EXPORT_SYMBOL(param_set_bint);
407
408const struct kernel_param_ops param_ops_bint = {
409 .flags = KERNEL_PARAM_OPS_FL_NOARG,
410 .set = param_set_bint,
411 .get = param_get_int,
412};
413EXPORT_SYMBOL(param_ops_bint);
414
415/* We break the rule and mangle the string. */
416static int param_array(struct module *mod,
417 const char *name,
418 const char *val,
419 unsigned int min, unsigned int max,
420 void *elem, int elemsize,
421 int (*set)(const char *, const struct kernel_param *kp),
422 s16 level,
423 unsigned int *num)
424{
425 int ret;
426 struct kernel_param kp;
427 char save;
428
429 /* Get the name right for errors. */
430 kp.name = name;
431 kp.arg = elem;
432 kp.level = level;
433
434 *num = 0;
435 /* We expect a comma-separated list of values. */
436 do {
437 int len;
438
439 if (*num == max) {
440 pr_err("%s: can only take %i arguments\n", name, max);
441 return -EINVAL;
442 }
443 len = strcspn(val, ",");
444
445 /* nul-terminate and parse */
446 save = val[len];
447 ((char *)val)[len] = '\0';
448 check_kparam_locked(mod);
449 ret = set(val, &kp);
450
451 if (ret != 0)
452 return ret;
453 kp.arg += elemsize;
454 val += len+1;
455 (*num)++;
456 } while (save == ',');
457
458 if (*num < min) {
459 pr_err("%s: needs at least %i arguments\n", name, min);
460 return -EINVAL;
461 }
462 return 0;
463}
464
465static int param_array_set(const char *val, const struct kernel_param *kp)
466{
467 const struct kparam_array *arr = kp->arr;
468 unsigned int temp_num;
469
470 return param_array(mod: kp->mod, name: kp->name, val, min: 1, max: arr->max, elem: arr->elem,
471 elemsize: arr->elemsize, set: arr->ops->set, level: kp->level,
472 num: arr->num ?: &temp_num);
473}
474
475static int param_array_get(char *buffer, const struct kernel_param *kp)
476{
477 int i, off, ret;
478 const struct kparam_array *arr = kp->arr;
479 struct kernel_param p = *kp;
480
481 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
482 /* Replace \n with comma */
483 if (i)
484 buffer[off - 1] = ',';
485 p.arg = arr->elem + arr->elemsize * i;
486 check_kparam_locked(mod: p.mod);
487 ret = arr->ops->get(buffer + off, &p);
488 if (ret < 0)
489 return ret;
490 off += ret;
491 }
492 buffer[off] = '\0';
493 return off;
494}
495
496static void param_array_free(void *arg)
497{
498 unsigned int i;
499 const struct kparam_array *arr = arg;
500
501 if (arr->ops->free)
502 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
503 arr->ops->free(arr->elem + arr->elemsize * i);
504}
505
506const struct kernel_param_ops param_array_ops = {
507 .set = param_array_set,
508 .get = param_array_get,
509 .free = param_array_free,
510};
511EXPORT_SYMBOL(param_array_ops);
512
513int param_set_copystring(const char *val, const struct kernel_param *kp)
514{
515 const struct kparam_string *kps = kp->str;
516 const size_t len = strnlen(val, kps->maxlen);
517
518 if (len == kps->maxlen) {
519 pr_err("%s: string doesn't fit in %u chars.\n",
520 kp->name, kps->maxlen-1);
521 return -ENOSPC;
522 }
523 memcpy(to: kps->string, from: val, len: len + 1);
524 return 0;
525}
526EXPORT_SYMBOL(param_set_copystring);
527
528int param_get_string(char *buffer, const struct kernel_param *kp)
529{
530 const struct kparam_string *kps = kp->str;
531 return scnprintf(buf: buffer, PAGE_SIZE, fmt: "%s\n", kps->string);
532}
533EXPORT_SYMBOL(param_get_string);
534
535const struct kernel_param_ops param_ops_string = {
536 .set = param_set_copystring,
537 .get = param_get_string,
538};
539EXPORT_SYMBOL(param_ops_string);
540
541/* sysfs output in /sys/modules/XYZ/parameters/ */
542#define to_module_attr(n) container_of_const(n, struct module_attribute, attr)
543#define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
544
545struct param_attribute
546{
547 struct module_attribute mattr;
548 const struct kernel_param *param;
549};
550
551struct module_param_attrs
552{
553 unsigned int num;
554 struct attribute_group grp;
555 struct param_attribute attrs[] __counted_by(num);
556};
557
558#ifdef CONFIG_SYSFS
559#define to_param_attr(n) container_of_const(n, struct param_attribute, mattr)
560
561static ssize_t param_attr_show(const struct module_attribute *mattr,
562 struct module_kobject *mk, char *buf)
563{
564 int count;
565 const struct param_attribute *attribute = to_param_attr(mattr);
566
567 if (!attribute->param->ops->get)
568 return -EPERM;
569
570 kernel_param_lock(mod: mk->mod);
571 count = attribute->param->ops->get(buf, attribute->param);
572 kernel_param_unlock(mod: mk->mod);
573 return count;
574}
575
576/* sysfs always hands a nul-terminated string in buf. We rely on that. */
577static ssize_t param_attr_store(const struct module_attribute *mattr,
578 struct module_kobject *mk,
579 const char *buf, size_t len)
580{
581 int err;
582 const struct param_attribute *attribute = to_param_attr(mattr);
583
584 if (!attribute->param->ops->set)
585 return -EPERM;
586
587 kernel_param_lock(mod: mk->mod);
588 if (param_check_unsafe(kp: attribute->param))
589 err = attribute->param->ops->set(buf, attribute->param);
590 else
591 err = -EPERM;
592 kernel_param_unlock(mod: mk->mod);
593 if (!err)
594 return len;
595 return err;
596}
597#endif
598
599#ifdef CONFIG_MODULES
600#define __modinit
601#else
602#define __modinit __init
603#endif
604
605#ifdef CONFIG_SYSFS
606void kernel_param_lock(struct module *mod)
607{
608 mutex_lock(KPARAM_MUTEX(mod));
609}
610
611void kernel_param_unlock(struct module *mod)
612{
613 mutex_unlock(KPARAM_MUTEX(mod));
614}
615
616EXPORT_SYMBOL(kernel_param_lock);
617EXPORT_SYMBOL(kernel_param_unlock);
618
619/*
620 * add_sysfs_param - add a parameter to sysfs
621 * @mk: struct module_kobject
622 * @kp: the actual parameter definition to add to sysfs
623 * @name: name of parameter
624 *
625 * Create a kobject if for a (per-module) parameter if mp NULL, and
626 * create file in sysfs. Returns an error on out of memory. Always cleans up
627 * if there's an error.
628 */
629static __modinit int add_sysfs_param(struct module_kobject *mk,
630 const struct kernel_param *kp,
631 const char *name)
632{
633 struct module_param_attrs *new_mp;
634 struct attribute **new_attrs;
635 unsigned int i;
636
637 /* We don't bother calling this with invisible parameters. */
638 BUG_ON(!kp->perm);
639
640 if (!mk->mp) {
641 /* First allocation. */
642 mk->mp = kzalloc(sizeof(*mk->mp), GFP_KERNEL);
643 if (!mk->mp)
644 return -ENOMEM;
645 mk->mp->grp.name = "parameters";
646 /* NULL-terminated attribute array. */
647 mk->mp->grp.attrs = kzalloc(sizeof(mk->mp->grp.attrs[0]),
648 GFP_KERNEL);
649 /* Caller will cleanup via free_module_param_attrs */
650 if (!mk->mp->grp.attrs)
651 return -ENOMEM;
652 }
653
654 /* Enlarge allocations. */
655 new_mp = krealloc(mk->mp, struct_size(mk->mp, attrs, mk->mp->num + 1),
656 GFP_KERNEL);
657 if (!new_mp)
658 return -ENOMEM;
659 mk->mp = new_mp;
660 mk->mp->num++;
661
662 /* Extra pointer for NULL terminator */
663 new_attrs = krealloc_array(mk->mp->grp.attrs, mk->mp->num + 1,
664 sizeof(mk->mp->grp.attrs[0]), GFP_KERNEL);
665 if (!new_attrs)
666 return -ENOMEM;
667 mk->mp->grp.attrs = new_attrs;
668
669 /* Tack new one on the end. */
670 memset(s: &mk->mp->attrs[mk->mp->num - 1], c: 0, n: sizeof(mk->mp->attrs[0]));
671 sysfs_attr_init(&mk->mp->attrs[mk->mp->num - 1].mattr.attr);
672 mk->mp->attrs[mk->mp->num - 1].param = kp;
673 mk->mp->attrs[mk->mp->num - 1].mattr.show = param_attr_show;
674 /* Do not allow runtime DAC changes to make param writable. */
675 if ((kp->perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
676 mk->mp->attrs[mk->mp->num - 1].mattr.store = param_attr_store;
677 else
678 mk->mp->attrs[mk->mp->num - 1].mattr.store = NULL;
679 mk->mp->attrs[mk->mp->num - 1].mattr.attr.name = (char *)name;
680 mk->mp->attrs[mk->mp->num - 1].mattr.attr.mode = kp->perm;
681
682 /* Fix up all the pointers, since krealloc can move us */
683 for (i = 0; i < mk->mp->num; i++)
684 mk->mp->grp.attrs[i] = &mk->mp->attrs[i].mattr.attr;
685 mk->mp->grp.attrs[mk->mp->num] = NULL;
686 return 0;
687}
688
689#ifdef CONFIG_MODULES
690static void free_module_param_attrs(struct module_kobject *mk)
691{
692 if (mk->mp)
693 kfree(objp: mk->mp->grp.attrs);
694 kfree(objp: mk->mp);
695 mk->mp = NULL;
696}
697
698/*
699 * module_param_sysfs_setup - setup sysfs support for one module
700 * @mod: module
701 * @kparam: module parameters (array)
702 * @num_params: number of module parameters
703 *
704 * Adds sysfs entries for module parameters under
705 * /sys/module/[mod->name]/parameters/
706 */
707int module_param_sysfs_setup(struct module *mod,
708 const struct kernel_param *kparam,
709 unsigned int num_params)
710{
711 int i, err;
712 bool params = false;
713
714 for (i = 0; i < num_params; i++) {
715 if (kparam[i].perm == 0)
716 continue;
717 err = add_sysfs_param(mk: &mod->mkobj, kp: &kparam[i], name: kparam[i].name);
718 if (err) {
719 free_module_param_attrs(mk: &mod->mkobj);
720 return err;
721 }
722 params = true;
723 }
724
725 if (!params)
726 return 0;
727
728 /* Create the param group. */
729 err = sysfs_create_group(kobj: &mod->mkobj.kobj, grp: &mod->mkobj.mp->grp);
730 if (err)
731 free_module_param_attrs(mk: &mod->mkobj);
732 return err;
733}
734
735/*
736 * module_param_sysfs_remove - remove sysfs support for one module
737 * @mod: module
738 *
739 * Remove sysfs entries for module parameters and the corresponding
740 * kobject.
741 */
742void module_param_sysfs_remove(struct module *mod)
743{
744 if (mod->mkobj.mp) {
745 sysfs_remove_group(kobj: &mod->mkobj.kobj, grp: &mod->mkobj.mp->grp);
746 /*
747 * We are positive that no one is using any param
748 * attrs at this point. Deallocate immediately.
749 */
750 free_module_param_attrs(mk: &mod->mkobj);
751 }
752}
753#endif
754
755void destroy_params(const struct kernel_param *params, unsigned num)
756{
757 unsigned int i;
758
759 for (i = 0; i < num; i++)
760 if (params[i].ops->free)
761 params[i].ops->free(params[i].arg);
762}
763
764struct module_kobject __modinit * lookup_or_create_module_kobject(const char *name)
765{
766 struct module_kobject *mk;
767 struct kobject *kobj;
768 int err;
769
770 kobj = kset_find_obj(module_kset, name);
771 if (kobj)
772 return to_module_kobject(kobj);
773
774 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
775 if (!mk)
776 return NULL;
777
778 mk->mod = THIS_MODULE;
779 mk->kobj.kset = module_kset;
780 err = kobject_init_and_add(kobj: &mk->kobj, ktype: &module_ktype, NULL, fmt: "%s", name);
781 if (IS_ENABLED(CONFIG_MODULES) && !err)
782 err = sysfs_create_file(kobj: &mk->kobj, attr: &module_uevent.attr);
783 if (err) {
784 kobject_put(kobj: &mk->kobj);
785 pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
786 name, err);
787 return NULL;
788 }
789
790 /* So that we hold reference in both cases. */
791 kobject_get(kobj: &mk->kobj);
792
793 return mk;
794}
795
796static void __init kernel_add_sysfs_param(const char *name,
797 const struct kernel_param *kparam,
798 unsigned int name_skip)
799{
800 struct module_kobject *mk;
801 int err;
802
803 mk = lookup_or_create_module_kobject(name);
804 if (!mk)
805 return;
806
807 /* We need to remove old parameters before adding more. */
808 if (mk->mp)
809 sysfs_remove_group(kobj: &mk->kobj, grp: &mk->mp->grp);
810
811 /* These should not fail at boot. */
812 err = add_sysfs_param(mk, kp: kparam, name: kparam->name + name_skip);
813 BUG_ON(err);
814 err = sysfs_create_group(kobj: &mk->kobj, grp: &mk->mp->grp);
815 BUG_ON(err);
816 kobject_uevent(kobj: &mk->kobj, action: KOBJ_ADD);
817 kobject_put(kobj: &mk->kobj);
818}
819
820/*
821 * param_sysfs_builtin - add sysfs parameters for built-in modules
822 *
823 * Add module_parameters to sysfs for "modules" built into the kernel.
824 *
825 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
826 * "parameter" name is stored behind a dot in kernel_param->name. So,
827 * extract the "module" name for all built-in kernel_param-eters,
828 * and for all who have the same, call kernel_add_sysfs_param.
829 */
830static void __init param_sysfs_builtin(void)
831{
832 const struct kernel_param *kp;
833 unsigned int name_len;
834 char modname[MODULE_NAME_LEN];
835
836 for (kp = __start___param; kp < __stop___param; kp++) {
837 char *dot;
838
839 if (kp->perm == 0)
840 continue;
841
842 dot = strchr(kp->name, '.');
843 if (!dot) {
844 /* This happens for core_param() */
845 strscpy(modname, "kernel");
846 name_len = 0;
847 } else {
848 name_len = dot - kp->name + 1;
849 strscpy(modname, kp->name, name_len);
850 }
851 kernel_add_sysfs_param(name: modname, kparam: kp, name_skip: name_len);
852 }
853}
854
855ssize_t __modver_version_show(const struct module_attribute *mattr,
856 struct module_kobject *mk, char *buf)
857{
858 const struct module_version_attribute *vattr =
859 container_of_const(mattr, struct module_version_attribute, mattr);
860
861 return scnprintf(buf, PAGE_SIZE, fmt: "%s\n", vattr->version);
862}
863
864extern const struct module_version_attribute __start___modver[];
865extern const struct module_version_attribute __stop___modver[];
866
867static void __init version_sysfs_builtin(void)
868{
869 const struct module_version_attribute *vattr;
870 struct module_kobject *mk;
871 int err;
872
873 for (vattr = __start___modver; vattr < __stop___modver; vattr++) {
874 mk = lookup_or_create_module_kobject(name: vattr->module_name);
875 if (mk) {
876 err = sysfs_create_file(kobj: &mk->kobj, attr: &vattr->mattr.attr);
877 WARN_ON_ONCE(err);
878 kobject_uevent(kobj: &mk->kobj, action: KOBJ_ADD);
879 kobject_put(kobj: &mk->kobj);
880 }
881 }
882}
883
884/* module-related sysfs stuff */
885
886static ssize_t module_attr_show(struct kobject *kobj,
887 struct attribute *attr,
888 char *buf)
889{
890 const struct module_attribute *attribute;
891 struct module_kobject *mk;
892 int ret;
893
894 attribute = to_module_attr(attr);
895 mk = to_module_kobject(kobj);
896
897 if (!attribute->show)
898 return -EIO;
899
900 ret = attribute->show(attribute, mk, buf);
901
902 return ret;
903}
904
905static ssize_t module_attr_store(struct kobject *kobj,
906 struct attribute *attr,
907 const char *buf, size_t len)
908{
909 const struct module_attribute *attribute;
910 struct module_kobject *mk;
911 int ret;
912
913 attribute = to_module_attr(attr);
914 mk = to_module_kobject(kobj);
915
916 if (!attribute->store)
917 return -EIO;
918
919 ret = attribute->store(attribute, mk, buf, len);
920
921 return ret;
922}
923
924static const struct sysfs_ops module_sysfs_ops = {
925 .show = module_attr_show,
926 .store = module_attr_store,
927};
928
929static int uevent_filter(const struct kobject *kobj)
930{
931 const struct kobj_type *ktype = get_ktype(kobj);
932
933 if (ktype == &module_ktype)
934 return 1;
935 return 0;
936}
937
938static const struct kset_uevent_ops module_uevent_ops = {
939 .filter = uevent_filter,
940};
941
942struct kset *module_kset;
943
944static void module_kobj_release(struct kobject *kobj)
945{
946 struct module_kobject *mk = to_module_kobject(kobj);
947
948 if (mk->kobj_completion)
949 complete(mk->kobj_completion);
950}
951
952const struct kobj_type module_ktype = {
953 .release = module_kobj_release,
954 .sysfs_ops = &module_sysfs_ops,
955};
956
957/*
958 * param_sysfs_init - create "module" kset
959 *
960 * This must be done before the initramfs is unpacked and
961 * request_module() thus becomes possible, because otherwise the
962 * module load would fail in mod_sysfs_init.
963 */
964static int __init param_sysfs_init(void)
965{
966 module_kset = kset_create_and_add(name: "module", u: &module_uevent_ops, NULL);
967 if (!module_kset) {
968 printk(KERN_WARNING "%s (%d): error creating kset\n",
969 __FILE__, __LINE__);
970 return -ENOMEM;
971 }
972
973 return 0;
974}
975subsys_initcall(param_sysfs_init);
976
977/*
978 * param_sysfs_builtin_init - add sysfs version and parameter
979 * attributes for built-in modules
980 */
981static int __init param_sysfs_builtin_init(void)
982{
983 if (!module_kset)
984 return -ENOMEM;
985
986 version_sysfs_builtin();
987 param_sysfs_builtin();
988
989 return 0;
990}
991late_initcall(param_sysfs_builtin_init);
992
993#endif /* CONFIG_SYSFS */
994