params.c 24 KB

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