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