params.c 24 KB

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