sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Module sysfs support
  4. *
  5. * Copyright (C) 2008 Rusty Russell
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/fs.h>
  10. #include <linux/sysfs.h>
  11. #include <linux/slab.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/mutex.h>
  14. #include "internal.h"
  15. /*
  16. * /sys/module/foo/sections stuff
  17. * J. Corbet <corbet@lwn.net>
  18. */
  19. #ifdef CONFIG_KALLSYMS
  20. struct module_sect_attr {
  21. struct bin_attribute battr;
  22. unsigned long address;
  23. };
  24. struct module_sect_attrs {
  25. struct attribute_group grp;
  26. unsigned int nsections;
  27. struct module_sect_attr attrs[];
  28. };
  29. #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
  30. static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
  31. struct bin_attribute *battr,
  32. char *buf, loff_t pos, size_t count)
  33. {
  34. struct module_sect_attr *sattr =
  35. container_of(battr, struct module_sect_attr, battr);
  36. char bounce[MODULE_SECT_READ_SIZE + 1];
  37. size_t wrote;
  38. if (pos != 0)
  39. return -EINVAL;
  40. /*
  41. * Since we're a binary read handler, we must account for the
  42. * trailing NUL byte that sprintf will write: if "buf" is
  43. * too small to hold the NUL, or the NUL is exactly the last
  44. * byte, the read will look like it got truncated by one byte.
  45. * Since there is no way to ask sprintf nicely to not write
  46. * the NUL, we have to use a bounce buffer.
  47. */
  48. wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
  49. kallsyms_show_value(file->f_cred)
  50. ? (void *)sattr->address : NULL);
  51. count = min(count, wrote);
  52. memcpy(buf, bounce, count);
  53. return count;
  54. }
  55. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  56. {
  57. unsigned int section;
  58. for (section = 0; section < sect_attrs->nsections; section++)
  59. kfree(sect_attrs->attrs[section].battr.attr.name);
  60. kfree(sect_attrs);
  61. }
  62. static int add_sect_attrs(struct module *mod, const struct load_info *info)
  63. {
  64. unsigned int nloaded = 0, i, size[2];
  65. struct module_sect_attrs *sect_attrs;
  66. struct module_sect_attr *sattr;
  67. struct bin_attribute **gattr;
  68. int ret;
  69. /* Count loaded sections and allocate structures */
  70. for (i = 0; i < info->hdr->e_shnum; i++)
  71. if (!sect_empty(&info->sechdrs[i]))
  72. nloaded++;
  73. size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
  74. sizeof(sect_attrs->grp.bin_attrs[0]));
  75. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
  76. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  77. if (!sect_attrs)
  78. return -ENOMEM;
  79. /* Setup section attributes. */
  80. sect_attrs->grp.name = "sections";
  81. sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
  82. sect_attrs->nsections = 0;
  83. sattr = &sect_attrs->attrs[0];
  84. gattr = &sect_attrs->grp.bin_attrs[0];
  85. for (i = 0; i < info->hdr->e_shnum; i++) {
  86. Elf_Shdr *sec = &info->sechdrs[i];
  87. if (sect_empty(sec))
  88. continue;
  89. sysfs_bin_attr_init(&sattr->battr);
  90. sattr->address = sec->sh_addr;
  91. sattr->battr.attr.name =
  92. kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
  93. if (!sattr->battr.attr.name) {
  94. ret = -ENOMEM;
  95. goto out;
  96. }
  97. sect_attrs->nsections++;
  98. sattr->battr.read = module_sect_read;
  99. sattr->battr.size = MODULE_SECT_READ_SIZE;
  100. sattr->battr.attr.mode = 0400;
  101. *(gattr++) = &(sattr++)->battr;
  102. }
  103. *gattr = NULL;
  104. ret = sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp);
  105. if (ret)
  106. goto out;
  107. mod->sect_attrs = sect_attrs;
  108. return 0;
  109. out:
  110. free_sect_attrs(sect_attrs);
  111. return ret;
  112. }
  113. static void remove_sect_attrs(struct module *mod)
  114. {
  115. if (mod->sect_attrs) {
  116. sysfs_remove_group(&mod->mkobj.kobj,
  117. &mod->sect_attrs->grp);
  118. /*
  119. * We are positive that no one is using any sect attrs
  120. * at this point. Deallocate immediately.
  121. */
  122. free_sect_attrs(mod->sect_attrs);
  123. mod->sect_attrs = NULL;
  124. }
  125. }
  126. /*
  127. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  128. */
  129. struct module_notes_attrs {
  130. struct kobject *dir;
  131. unsigned int notes;
  132. struct bin_attribute attrs[] __counted_by(notes);
  133. };
  134. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  135. unsigned int i)
  136. {
  137. if (notes_attrs->dir) {
  138. while (i-- > 0)
  139. sysfs_remove_bin_file(notes_attrs->dir,
  140. &notes_attrs->attrs[i]);
  141. kobject_put(notes_attrs->dir);
  142. }
  143. kfree(notes_attrs);
  144. }
  145. static int add_notes_attrs(struct module *mod, const struct load_info *info)
  146. {
  147. unsigned int notes, loaded, i;
  148. struct module_notes_attrs *notes_attrs;
  149. struct bin_attribute *nattr;
  150. int ret;
  151. /* Count notes sections and allocate structures. */
  152. notes = 0;
  153. for (i = 0; i < info->hdr->e_shnum; i++)
  154. if (!sect_empty(&info->sechdrs[i]) &&
  155. info->sechdrs[i].sh_type == SHT_NOTE)
  156. ++notes;
  157. if (notes == 0)
  158. return 0;
  159. notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
  160. GFP_KERNEL);
  161. if (!notes_attrs)
  162. return -ENOMEM;
  163. notes_attrs->notes = notes;
  164. nattr = &notes_attrs->attrs[0];
  165. for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
  166. if (sect_empty(&info->sechdrs[i]))
  167. continue;
  168. if (info->sechdrs[i].sh_type == SHT_NOTE) {
  169. sysfs_bin_attr_init(nattr);
  170. nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
  171. nattr->attr.mode = 0444;
  172. nattr->size = info->sechdrs[i].sh_size;
  173. nattr->private = (void *)info->sechdrs[i].sh_addr;
  174. nattr->read = sysfs_bin_attr_simple_read;
  175. ++nattr;
  176. }
  177. ++loaded;
  178. }
  179. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  180. if (!notes_attrs->dir) {
  181. ret = -ENOMEM;
  182. goto out;
  183. }
  184. for (i = 0; i < notes; ++i) {
  185. ret = sysfs_create_bin_file(notes_attrs->dir, &notes_attrs->attrs[i]);
  186. if (ret)
  187. goto out;
  188. }
  189. mod->notes_attrs = notes_attrs;
  190. return 0;
  191. out:
  192. free_notes_attrs(notes_attrs, i);
  193. return ret;
  194. }
  195. static void remove_notes_attrs(struct module *mod)
  196. {
  197. if (mod->notes_attrs)
  198. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  199. }
  200. #else /* !CONFIG_KALLSYMS */
  201. static inline int add_sect_attrs(struct module *mod, const struct load_info *info)
  202. {
  203. return 0;
  204. }
  205. static inline void remove_sect_attrs(struct module *mod) { }
  206. static inline int add_notes_attrs(struct module *mod, const struct load_info *info)
  207. {
  208. return 0;
  209. }
  210. static inline void remove_notes_attrs(struct module *mod) { }
  211. #endif /* CONFIG_KALLSYMS */
  212. static void del_usage_links(struct module *mod)
  213. {
  214. #ifdef CONFIG_MODULE_UNLOAD
  215. struct module_use *use;
  216. mutex_lock(&module_mutex);
  217. list_for_each_entry(use, &mod->target_list, target_list)
  218. sysfs_remove_link(use->target->holders_dir, mod->name);
  219. mutex_unlock(&module_mutex);
  220. #endif
  221. }
  222. static int add_usage_links(struct module *mod)
  223. {
  224. int ret = 0;
  225. #ifdef CONFIG_MODULE_UNLOAD
  226. struct module_use *use;
  227. mutex_lock(&module_mutex);
  228. list_for_each_entry(use, &mod->target_list, target_list) {
  229. ret = sysfs_create_link(use->target->holders_dir,
  230. &mod->mkobj.kobj, mod->name);
  231. if (ret)
  232. break;
  233. }
  234. mutex_unlock(&module_mutex);
  235. if (ret)
  236. del_usage_links(mod);
  237. #endif
  238. return ret;
  239. }
  240. static void module_remove_modinfo_attrs(struct module *mod, int end)
  241. {
  242. struct module_attribute *attr;
  243. int i;
  244. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  245. if (end >= 0 && i > end)
  246. break;
  247. /* pick a field to test for end of list */
  248. if (!attr->attr.name)
  249. break;
  250. sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
  251. if (attr->free)
  252. attr->free(mod);
  253. }
  254. kfree(mod->modinfo_attrs);
  255. }
  256. static int module_add_modinfo_attrs(struct module *mod)
  257. {
  258. struct module_attribute *attr;
  259. struct module_attribute *temp_attr;
  260. int error = 0;
  261. int i;
  262. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  263. (modinfo_attrs_count + 1)),
  264. GFP_KERNEL);
  265. if (!mod->modinfo_attrs)
  266. return -ENOMEM;
  267. temp_attr = mod->modinfo_attrs;
  268. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  269. if (!attr->test || attr->test(mod)) {
  270. memcpy(temp_attr, attr, sizeof(*temp_attr));
  271. sysfs_attr_init(&temp_attr->attr);
  272. error = sysfs_create_file(&mod->mkobj.kobj,
  273. &temp_attr->attr);
  274. if (error)
  275. goto error_out;
  276. ++temp_attr;
  277. }
  278. }
  279. return 0;
  280. error_out:
  281. if (i > 0)
  282. module_remove_modinfo_attrs(mod, --i);
  283. else
  284. kfree(mod->modinfo_attrs);
  285. return error;
  286. }
  287. static void mod_kobject_put(struct module *mod)
  288. {
  289. DECLARE_COMPLETION_ONSTACK(c);
  290. mod->mkobj.kobj_completion = &c;
  291. kobject_put(&mod->mkobj.kobj);
  292. wait_for_completion(&c);
  293. }
  294. static int mod_sysfs_init(struct module *mod)
  295. {
  296. int err;
  297. struct kobject *kobj;
  298. if (!module_kset) {
  299. pr_err("%s: module sysfs not initialized\n", mod->name);
  300. err = -EINVAL;
  301. goto out;
  302. }
  303. kobj = kset_find_obj(module_kset, mod->name);
  304. if (kobj) {
  305. pr_err("%s: module is already loaded\n", mod->name);
  306. kobject_put(kobj);
  307. err = -EINVAL;
  308. goto out;
  309. }
  310. mod->mkobj.mod = mod;
  311. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  312. mod->mkobj.kobj.kset = module_kset;
  313. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  314. "%s", mod->name);
  315. if (err)
  316. mod_kobject_put(mod);
  317. out:
  318. return err;
  319. }
  320. int mod_sysfs_setup(struct module *mod,
  321. const struct load_info *info,
  322. struct kernel_param *kparam,
  323. unsigned int num_params)
  324. {
  325. int err;
  326. err = mod_sysfs_init(mod);
  327. if (err)
  328. goto out;
  329. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  330. if (!mod->holders_dir) {
  331. err = -ENOMEM;
  332. goto out_unreg;
  333. }
  334. err = module_param_sysfs_setup(mod, kparam, num_params);
  335. if (err)
  336. goto out_unreg_holders;
  337. err = module_add_modinfo_attrs(mod);
  338. if (err)
  339. goto out_unreg_param;
  340. err = add_usage_links(mod);
  341. if (err)
  342. goto out_unreg_modinfo_attrs;
  343. err = add_sect_attrs(mod, info);
  344. if (err)
  345. goto out_del_usage_links;
  346. err = add_notes_attrs(mod, info);
  347. if (err)
  348. goto out_unreg_sect_attrs;
  349. return 0;
  350. out_unreg_sect_attrs:
  351. remove_sect_attrs(mod);
  352. out_del_usage_links:
  353. del_usage_links(mod);
  354. out_unreg_modinfo_attrs:
  355. module_remove_modinfo_attrs(mod, -1);
  356. out_unreg_param:
  357. module_param_sysfs_remove(mod);
  358. out_unreg_holders:
  359. kobject_put(mod->holders_dir);
  360. out_unreg:
  361. mod_kobject_put(mod);
  362. out:
  363. return err;
  364. }
  365. static void mod_sysfs_fini(struct module *mod)
  366. {
  367. remove_notes_attrs(mod);
  368. remove_sect_attrs(mod);
  369. mod_kobject_put(mod);
  370. }
  371. void mod_sysfs_teardown(struct module *mod)
  372. {
  373. del_usage_links(mod);
  374. module_remove_modinfo_attrs(mod, -1);
  375. module_param_sysfs_remove(mod);
  376. kobject_put(mod->mkobj.drivers_dir);
  377. kobject_put(mod->holders_dir);
  378. mod_sysfs_fini(mod);
  379. }
  380. void init_param_lock(struct module *mod)
  381. {
  382. mutex_init(&mod->param_lock);
  383. }