params.c 23 KB

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