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