params.c 24 KB

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