codetag.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/codetag.h>
  3. #include <linux/idr.h>
  4. #include <linux/kallsyms.h>
  5. #include <linux/module.h>
  6. #include <linux/seq_buf.h>
  7. #include <linux/slab.h>
  8. #include <linux/vmalloc.h>
  9. struct codetag_type {
  10. struct list_head link;
  11. unsigned int count;
  12. struct idr mod_idr;
  13. /*
  14. * protects mod_idr, next_mod_seq,
  15. * iter->mod_seq and cmod->mod_seq
  16. */
  17. struct rw_semaphore mod_lock;
  18. struct codetag_type_desc desc;
  19. /* generates unique sequence number for module load */
  20. unsigned long next_mod_seq;
  21. };
  22. struct codetag_range {
  23. struct codetag *start;
  24. struct codetag *stop;
  25. };
  26. struct codetag_module {
  27. struct module *mod;
  28. struct codetag_range range;
  29. unsigned long mod_seq;
  30. };
  31. static DEFINE_MUTEX(codetag_lock);
  32. static LIST_HEAD(codetag_types);
  33. void codetag_lock_module_list(struct codetag_type *cttype, bool lock)
  34. {
  35. if (lock)
  36. down_read(&cttype->mod_lock);
  37. else
  38. up_read(&cttype->mod_lock);
  39. }
  40. bool codetag_trylock_module_list(struct codetag_type *cttype)
  41. {
  42. return down_read_trylock(&cttype->mod_lock) != 0;
  43. }
  44. struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype)
  45. {
  46. struct codetag_iterator iter = {
  47. .cttype = cttype,
  48. .cmod = NULL,
  49. .mod_id = 0,
  50. .ct = NULL,
  51. .mod_seq = 0,
  52. };
  53. return iter;
  54. }
  55. static inline struct codetag *get_first_module_ct(struct codetag_module *cmod)
  56. {
  57. return cmod->range.start < cmod->range.stop ? cmod->range.start : NULL;
  58. }
  59. static inline
  60. struct codetag *get_next_module_ct(struct codetag_iterator *iter)
  61. {
  62. struct codetag *res = (struct codetag *)
  63. ((char *)iter->ct + iter->cttype->desc.tag_size);
  64. return res < iter->cmod->range.stop ? res : NULL;
  65. }
  66. struct codetag *codetag_next_ct(struct codetag_iterator *iter)
  67. {
  68. struct codetag_type *cttype = iter->cttype;
  69. struct codetag_module *cmod;
  70. struct codetag *ct;
  71. lockdep_assert_held(&cttype->mod_lock);
  72. if (unlikely(idr_is_empty(&cttype->mod_idr)))
  73. return NULL;
  74. ct = NULL;
  75. while (true) {
  76. cmod = idr_find(&cttype->mod_idr, iter->mod_id);
  77. /* If module was removed move to the next one */
  78. if (!cmod)
  79. cmod = idr_get_next_ul(&cttype->mod_idr,
  80. &iter->mod_id);
  81. /* Exit if no more modules */
  82. if (!cmod)
  83. break;
  84. if (!iter->cmod || iter->mod_seq != cmod->mod_seq) {
  85. iter->cmod = cmod;
  86. iter->mod_seq = cmod->mod_seq;
  87. ct = get_first_module_ct(cmod);
  88. } else {
  89. ct = get_next_module_ct(iter);
  90. }
  91. if (ct)
  92. break;
  93. iter->mod_id++;
  94. }
  95. iter->ct = ct;
  96. return ct;
  97. }
  98. void codetag_to_text(struct seq_buf *out, struct codetag *ct)
  99. {
  100. if (ct->modname)
  101. seq_buf_printf(out, "%s:%u [%s] func:%s",
  102. ct->filename, ct->lineno,
  103. ct->modname, ct->function);
  104. else
  105. seq_buf_printf(out, "%s:%u func:%s",
  106. ct->filename, ct->lineno, ct->function);
  107. }
  108. static inline size_t range_size(const struct codetag_type *cttype,
  109. const struct codetag_range *range)
  110. {
  111. return ((char *)range->stop - (char *)range->start) /
  112. cttype->desc.tag_size;
  113. }
  114. static void *get_symbol(struct module *mod, const char *prefix, const char *name)
  115. {
  116. DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN);
  117. const char *buf;
  118. void *ret;
  119. seq_buf_printf(&sb, "%s%s", prefix, name);
  120. if (seq_buf_has_overflowed(&sb))
  121. return NULL;
  122. buf = seq_buf_str(&sb);
  123. preempt_disable();
  124. ret = mod ?
  125. (void *)find_kallsyms_symbol_value(mod, buf) :
  126. (void *)kallsyms_lookup_name(buf);
  127. preempt_enable();
  128. return ret;
  129. }
  130. static struct codetag_range get_section_range(struct module *mod,
  131. const char *section)
  132. {
  133. return (struct codetag_range) {
  134. get_symbol(mod, CODETAG_SECTION_START_PREFIX, section),
  135. get_symbol(mod, CODETAG_SECTION_STOP_PREFIX, section),
  136. };
  137. }
  138. static const char *get_mod_name(__maybe_unused struct module *mod)
  139. {
  140. #ifdef CONFIG_MODULES
  141. if (mod)
  142. return mod->name;
  143. #endif
  144. return "(built-in)";
  145. }
  146. static int codetag_module_init(struct codetag_type *cttype, struct module *mod)
  147. {
  148. struct codetag_range range;
  149. struct codetag_module *cmod;
  150. int mod_id;
  151. int err;
  152. range = get_section_range(mod, cttype->desc.section);
  153. if (!range.start || !range.stop) {
  154. pr_warn("Failed to load code tags of type %s from the module %s\n",
  155. cttype->desc.section, get_mod_name(mod));
  156. return -EINVAL;
  157. }
  158. /* Ignore empty ranges */
  159. if (range.start == range.stop)
  160. return 0;
  161. BUG_ON(range.start > range.stop);
  162. cmod = kmalloc_obj(*cmod);
  163. if (unlikely(!cmod))
  164. return -ENOMEM;
  165. cmod->mod = mod;
  166. cmod->range = range;
  167. down_write(&cttype->mod_lock);
  168. cmod->mod_seq = ++cttype->next_mod_seq;
  169. mod_id = idr_alloc(&cttype->mod_idr, cmod, 0, 0, GFP_KERNEL);
  170. if (mod_id >= 0) {
  171. if (cttype->desc.module_load) {
  172. err = cttype->desc.module_load(mod, range.start, range.stop);
  173. if (!err)
  174. cttype->count += range_size(cttype, &range);
  175. else
  176. idr_remove(&cttype->mod_idr, mod_id);
  177. } else {
  178. cttype->count += range_size(cttype, &range);
  179. err = 0;
  180. }
  181. } else {
  182. err = mod_id;
  183. }
  184. up_write(&cttype->mod_lock);
  185. if (err < 0) {
  186. kfree(cmod);
  187. return err;
  188. }
  189. return 0;
  190. }
  191. #ifdef CONFIG_MODULES
  192. #define CODETAG_SECTION_PREFIX ".codetag."
  193. /* Some codetag types need a separate module section */
  194. bool codetag_needs_module_section(struct module *mod, const char *name,
  195. unsigned long size)
  196. {
  197. const char *type_name;
  198. struct codetag_type *cttype;
  199. bool ret = false;
  200. if (strncmp(name, CODETAG_SECTION_PREFIX, strlen(CODETAG_SECTION_PREFIX)))
  201. return false;
  202. type_name = name + strlen(CODETAG_SECTION_PREFIX);
  203. mutex_lock(&codetag_lock);
  204. list_for_each_entry(cttype, &codetag_types, link) {
  205. if (strcmp(type_name, cttype->desc.section) == 0) {
  206. if (!cttype->desc.needs_section_mem)
  207. break;
  208. down_write(&cttype->mod_lock);
  209. ret = cttype->desc.needs_section_mem(mod, size);
  210. up_write(&cttype->mod_lock);
  211. break;
  212. }
  213. }
  214. mutex_unlock(&codetag_lock);
  215. return ret;
  216. }
  217. void *codetag_alloc_module_section(struct module *mod, const char *name,
  218. unsigned long size, unsigned int prepend,
  219. unsigned long align)
  220. {
  221. const char *type_name = name + strlen(CODETAG_SECTION_PREFIX);
  222. struct codetag_type *cttype;
  223. void *ret = ERR_PTR(-EINVAL);
  224. mutex_lock(&codetag_lock);
  225. list_for_each_entry(cttype, &codetag_types, link) {
  226. if (strcmp(type_name, cttype->desc.section) == 0) {
  227. if (WARN_ON(!cttype->desc.alloc_section_mem))
  228. break;
  229. down_write(&cttype->mod_lock);
  230. ret = cttype->desc.alloc_section_mem(mod, size, prepend, align);
  231. up_write(&cttype->mod_lock);
  232. break;
  233. }
  234. }
  235. mutex_unlock(&codetag_lock);
  236. return ret;
  237. }
  238. void codetag_free_module_sections(struct module *mod)
  239. {
  240. struct codetag_type *cttype;
  241. mutex_lock(&codetag_lock);
  242. list_for_each_entry(cttype, &codetag_types, link) {
  243. if (!cttype->desc.free_section_mem)
  244. continue;
  245. down_write(&cttype->mod_lock);
  246. cttype->desc.free_section_mem(mod, false);
  247. up_write(&cttype->mod_lock);
  248. }
  249. mutex_unlock(&codetag_lock);
  250. }
  251. void codetag_module_replaced(struct module *mod, struct module *new_mod)
  252. {
  253. struct codetag_type *cttype;
  254. mutex_lock(&codetag_lock);
  255. list_for_each_entry(cttype, &codetag_types, link) {
  256. if (!cttype->desc.module_replaced)
  257. continue;
  258. down_write(&cttype->mod_lock);
  259. cttype->desc.module_replaced(mod, new_mod);
  260. up_write(&cttype->mod_lock);
  261. }
  262. mutex_unlock(&codetag_lock);
  263. }
  264. int codetag_load_module(struct module *mod)
  265. {
  266. struct codetag_type *cttype;
  267. int ret = 0;
  268. if (!mod)
  269. return 0;
  270. mutex_lock(&codetag_lock);
  271. list_for_each_entry(cttype, &codetag_types, link) {
  272. ret = codetag_module_init(cttype, mod);
  273. if (ret)
  274. break;
  275. }
  276. mutex_unlock(&codetag_lock);
  277. return ret;
  278. }
  279. void codetag_unload_module(struct module *mod)
  280. {
  281. struct codetag_type *cttype;
  282. if (!mod)
  283. return;
  284. /* await any module's kfree_rcu() operations to complete */
  285. kvfree_rcu_barrier();
  286. mutex_lock(&codetag_lock);
  287. list_for_each_entry(cttype, &codetag_types, link) {
  288. struct codetag_module *found = NULL;
  289. struct codetag_module *cmod;
  290. unsigned long mod_id, tmp;
  291. down_write(&cttype->mod_lock);
  292. idr_for_each_entry_ul(&cttype->mod_idr, cmod, tmp, mod_id) {
  293. if (cmod->mod && cmod->mod == mod) {
  294. found = cmod;
  295. break;
  296. }
  297. }
  298. if (found) {
  299. if (cttype->desc.module_unload)
  300. cttype->desc.module_unload(cmod->mod,
  301. cmod->range.start, cmod->range.stop);
  302. cttype->count -= range_size(cttype, &cmod->range);
  303. idr_remove(&cttype->mod_idr, mod_id);
  304. kfree(cmod);
  305. }
  306. up_write(&cttype->mod_lock);
  307. if (found && cttype->desc.free_section_mem)
  308. cttype->desc.free_section_mem(mod, true);
  309. }
  310. mutex_unlock(&codetag_lock);
  311. }
  312. #endif /* CONFIG_MODULES */
  313. struct codetag_type *
  314. codetag_register_type(const struct codetag_type_desc *desc)
  315. {
  316. struct codetag_type *cttype;
  317. int err;
  318. BUG_ON(desc->tag_size <= 0);
  319. cttype = kzalloc_obj(*cttype);
  320. if (unlikely(!cttype))
  321. return ERR_PTR(-ENOMEM);
  322. cttype->desc = *desc;
  323. idr_init(&cttype->mod_idr);
  324. init_rwsem(&cttype->mod_lock);
  325. err = codetag_module_init(cttype, NULL);
  326. if (unlikely(err)) {
  327. kfree(cttype);
  328. return ERR_PTR(err);
  329. }
  330. mutex_lock(&codetag_lock);
  331. list_add_tail(&cttype->link, &codetag_types);
  332. mutex_unlock(&codetag_lock);
  333. return cttype;
  334. }