params.c 23 KB

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