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