sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 <[email protected]>
  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 void 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. /* Count loaded sections and allocate structures */
  69. for (i = 0; i < info->hdr->e_shnum; i++)
  70. if (!sect_empty(&info->sechdrs[i]))
  71. nloaded++;
  72. size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
  73. sizeof(sect_attrs->grp.bin_attrs[0]));
  74. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
  75. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  76. if (!sect_attrs)
  77. return;
  78. /* Setup section attributes. */
  79. sect_attrs->grp.name = "sections";
  80. sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
  81. sect_attrs->nsections = 0;
  82. sattr = &sect_attrs->attrs[0];
  83. gattr = &sect_attrs->grp.bin_attrs[0];
  84. for (i = 0; i < info->hdr->e_shnum; i++) {
  85. Elf_Shdr *sec = &info->sechdrs[i];
  86. if (sect_empty(sec))
  87. continue;
  88. sysfs_bin_attr_init(&sattr->battr);
  89. sattr->address = sec->sh_addr;
  90. sattr->battr.attr.name =
  91. kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
  92. if (!sattr->battr.attr.name)
  93. goto out;
  94. sect_attrs->nsections++;
  95. sattr->battr.read = module_sect_read;
  96. sattr->battr.size = MODULE_SECT_READ_SIZE;
  97. sattr->battr.attr.mode = 0400;
  98. *(gattr++) = &(sattr++)->battr;
  99. }
  100. *gattr = NULL;
  101. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  102. goto out;
  103. mod->sect_attrs = sect_attrs;
  104. return;
  105. out:
  106. free_sect_attrs(sect_attrs);
  107. }
  108. static void remove_sect_attrs(struct module *mod)
  109. {
  110. if (mod->sect_attrs) {
  111. sysfs_remove_group(&mod->mkobj.kobj,
  112. &mod->sect_attrs->grp);
  113. /*
  114. * We are positive that no one is using any sect attrs
  115. * at this point. Deallocate immediately.
  116. */
  117. free_sect_attrs(mod->sect_attrs);
  118. mod->sect_attrs = NULL;
  119. }
  120. }
  121. /*
  122. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  123. */
  124. struct module_notes_attrs {
  125. struct kobject *dir;
  126. unsigned int notes;
  127. struct bin_attribute attrs[];
  128. };
  129. static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
  130. struct bin_attribute *bin_attr,
  131. char *buf, loff_t pos, size_t count)
  132. {
  133. /*
  134. * The caller checked the pos and count against our size.
  135. */
  136. memcpy(buf, bin_attr->private + pos, count);
  137. return count;
  138. }
  139. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  140. unsigned int i)
  141. {
  142. if (notes_attrs->dir) {
  143. while (i-- > 0)
  144. sysfs_remove_bin_file(notes_attrs->dir,
  145. &notes_attrs->attrs[i]);
  146. kobject_put(notes_attrs->dir);
  147. }
  148. kfree(notes_attrs);
  149. }
  150. static void add_notes_attrs(struct module *mod, const struct load_info *info)
  151. {
  152. unsigned int notes, loaded, i;
  153. struct module_notes_attrs *notes_attrs;
  154. struct bin_attribute *nattr;
  155. /* failed to create section attributes, so can't create notes */
  156. if (!mod->sect_attrs)
  157. return;
  158. /* Count notes sections and allocate structures. */
  159. notes = 0;
  160. for (i = 0; i < info->hdr->e_shnum; i++)
  161. if (!sect_empty(&info->sechdrs[i]) &&
  162. info->sechdrs[i].sh_type == SHT_NOTE)
  163. ++notes;
  164. if (notes == 0)
  165. return;
  166. notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
  167. GFP_KERNEL);
  168. if (!notes_attrs)
  169. return;
  170. notes_attrs->notes = notes;
  171. nattr = &notes_attrs->attrs[0];
  172. for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
  173. if (sect_empty(&info->sechdrs[i]))
  174. continue;
  175. if (info->sechdrs[i].sh_type == SHT_NOTE) {
  176. sysfs_bin_attr_init(nattr);
  177. nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
  178. nattr->attr.mode = 0444;
  179. nattr->size = info->sechdrs[i].sh_size;
  180. nattr->private = (void *)info->sechdrs[i].sh_addr;
  181. nattr->read = module_notes_read;
  182. ++nattr;
  183. }
  184. ++loaded;
  185. }
  186. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  187. if (!notes_attrs->dir)
  188. goto out;
  189. for (i = 0; i < notes; ++i)
  190. if (sysfs_create_bin_file(notes_attrs->dir,
  191. &notes_attrs->attrs[i]))
  192. goto out;
  193. mod->notes_attrs = notes_attrs;
  194. return;
  195. out:
  196. free_notes_attrs(notes_attrs, i);
  197. }
  198. static void remove_notes_attrs(struct module *mod)
  199. {
  200. if (mod->notes_attrs)
  201. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  202. }
  203. #else /* !CONFIG_KALLSYMS */
  204. static inline void add_sect_attrs(struct module *mod, const struct load_info *info) { }
  205. static inline void remove_sect_attrs(struct module *mod) { }
  206. static inline void add_notes_attrs(struct module *mod, const struct load_info *info) { }
  207. static inline void remove_notes_attrs(struct module *mod) { }
  208. #endif /* CONFIG_KALLSYMS */
  209. static void del_usage_links(struct module *mod)
  210. {
  211. #ifdef CONFIG_MODULE_UNLOAD
  212. struct module_use *use;
  213. mutex_lock(&module_mutex);
  214. list_for_each_entry(use, &mod->target_list, target_list)
  215. sysfs_remove_link(use->target->holders_dir, mod->name);
  216. mutex_unlock(&module_mutex);
  217. #endif
  218. }
  219. static int add_usage_links(struct module *mod)
  220. {
  221. int ret = 0;
  222. #ifdef CONFIG_MODULE_UNLOAD
  223. struct module_use *use;
  224. mutex_lock(&module_mutex);
  225. list_for_each_entry(use, &mod->target_list, target_list) {
  226. ret = sysfs_create_link(use->target->holders_dir,
  227. &mod->mkobj.kobj, mod->name);
  228. if (ret)
  229. break;
  230. }
  231. mutex_unlock(&module_mutex);
  232. if (ret)
  233. del_usage_links(mod);
  234. #endif
  235. return ret;
  236. }
  237. static void module_remove_modinfo_attrs(struct module *mod, int end)
  238. {
  239. struct module_attribute *attr;
  240. int i;
  241. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  242. if (end >= 0 && i > end)
  243. break;
  244. /* pick a field to test for end of list */
  245. if (!attr->attr.name)
  246. break;
  247. sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
  248. if (attr->free)
  249. attr->free(mod);
  250. }
  251. kfree(mod->modinfo_attrs);
  252. }
  253. static int module_add_modinfo_attrs(struct module *mod)
  254. {
  255. struct module_attribute *attr;
  256. struct module_attribute *temp_attr;
  257. int error = 0;
  258. int i;
  259. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  260. (modinfo_attrs_count + 1)),
  261. GFP_KERNEL);
  262. if (!mod->modinfo_attrs)
  263. return -ENOMEM;
  264. temp_attr = mod->modinfo_attrs;
  265. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  266. if (!attr->test || attr->test(mod)) {
  267. memcpy(temp_attr, attr, sizeof(*temp_attr));
  268. sysfs_attr_init(&temp_attr->attr);
  269. error = sysfs_create_file(&mod->mkobj.kobj,
  270. &temp_attr->attr);
  271. if (error)
  272. goto error_out;
  273. ++temp_attr;
  274. }
  275. }
  276. return 0;
  277. error_out:
  278. if (i > 0)
  279. module_remove_modinfo_attrs(mod, --i);
  280. else
  281. kfree(mod->modinfo_attrs);
  282. return error;
  283. }
  284. static void mod_kobject_put(struct module *mod)
  285. {
  286. DECLARE_COMPLETION_ONSTACK(c);
  287. mod->mkobj.kobj_completion = &c;
  288. kobject_put(&mod->mkobj.kobj);
  289. wait_for_completion(&c);
  290. }
  291. static int mod_sysfs_init(struct module *mod)
  292. {
  293. int err;
  294. struct kobject *kobj;
  295. if (!module_sysfs_initialized) {
  296. pr_err("%s: module sysfs not initialized\n", mod->name);
  297. err = -EINVAL;
  298. goto out;
  299. }
  300. kobj = kset_find_obj(module_kset, mod->name);
  301. if (kobj) {
  302. pr_err("%s: module is already loaded\n", mod->name);
  303. kobject_put(kobj);
  304. err = -EINVAL;
  305. goto out;
  306. }
  307. mod->mkobj.mod = mod;
  308. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  309. mod->mkobj.kobj.kset = module_kset;
  310. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  311. "%s", mod->name);
  312. if (err)
  313. mod_kobject_put(mod);
  314. out:
  315. return err;
  316. }
  317. int mod_sysfs_setup(struct module *mod,
  318. const struct load_info *info,
  319. struct kernel_param *kparam,
  320. unsigned int num_params)
  321. {
  322. int err;
  323. err = mod_sysfs_init(mod);
  324. if (err)
  325. goto out;
  326. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  327. if (!mod->holders_dir) {
  328. err = -ENOMEM;
  329. goto out_unreg;
  330. }
  331. err = module_param_sysfs_setup(mod, kparam, num_params);
  332. if (err)
  333. goto out_unreg_holders;
  334. err = module_add_modinfo_attrs(mod);
  335. if (err)
  336. goto out_unreg_param;
  337. err = add_usage_links(mod);
  338. if (err)
  339. goto out_unreg_modinfo_attrs;
  340. add_sect_attrs(mod, info);
  341. add_notes_attrs(mod, info);
  342. return 0;
  343. out_unreg_modinfo_attrs:
  344. module_remove_modinfo_attrs(mod, -1);
  345. out_unreg_param:
  346. module_param_sysfs_remove(mod);
  347. out_unreg_holders:
  348. kobject_put(mod->holders_dir);
  349. out_unreg:
  350. mod_kobject_put(mod);
  351. out:
  352. return err;
  353. }
  354. static void mod_sysfs_fini(struct module *mod)
  355. {
  356. remove_notes_attrs(mod);
  357. remove_sect_attrs(mod);
  358. mod_kobject_put(mod);
  359. }
  360. void mod_sysfs_teardown(struct module *mod)
  361. {
  362. del_usage_links(mod);
  363. module_remove_modinfo_attrs(mod, -1);
  364. module_param_sysfs_remove(mod);
  365. kobject_put(mod->mkobj.drivers_dir);
  366. kobject_put(mod->holders_dir);
  367. mod_sysfs_fini(mod);
  368. }
  369. void init_param_lock(struct module *mod)
  370. {
  371. mutex_init(&mod->param_lock);
  372. }