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