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