params.c 24 KB

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