params.c 23 KB

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