jump_label.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * jump label support
  3. *
  4. * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
  5. *
  6. */
  7. #include <linux/jump_label.h>
  8. #include <linux/memory.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/module.h>
  11. #include <linux/list.h>
  12. #include <linux/jhash.h>
  13. #include <linux/slab.h>
  14. #include <linux/sort.h>
  15. #include <linux/err.h>
  16. #ifdef HAVE_JUMP_LABEL
  17. #define JUMP_LABEL_HASH_BITS 6
  18. #define JUMP_LABEL_TABLE_SIZE (1 << JUMP_LABEL_HASH_BITS)
  19. static struct hlist_head jump_label_table[JUMP_LABEL_TABLE_SIZE];
  20. /* mutex to protect coming/going of the the jump_label table */
  21. static DEFINE_MUTEX(jump_label_mutex);
  22. struct jump_label_entry {
  23. struct hlist_node hlist;
  24. struct jump_entry *table;
  25. int nr_entries;
  26. /* hang modules off here */
  27. struct hlist_head modules;
  28. unsigned long key;
  29. };
  30. struct jump_label_module_entry {
  31. struct hlist_node hlist;
  32. struct jump_entry *table;
  33. int nr_entries;
  34. struct module *mod;
  35. };
  36. static int jump_label_cmp(const void *a, const void *b)
  37. {
  38. const struct jump_entry *jea = a;
  39. const struct jump_entry *jeb = b;
  40. if (jea->key < jeb->key)
  41. return -1;
  42. if (jea->key > jeb->key)
  43. return 1;
  44. return 0;
  45. }
  46. static void
  47. sort_jump_label_entries(struct jump_entry *start, struct jump_entry *stop)
  48. {
  49. unsigned long size;
  50. size = (((unsigned long)stop - (unsigned long)start)
  51. / sizeof(struct jump_entry));
  52. sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
  53. }
  54. static struct jump_label_entry *get_jump_label_entry(jump_label_t key)
  55. {
  56. struct hlist_head *head;
  57. struct hlist_node *node;
  58. struct jump_label_entry *e;
  59. u32 hash = jhash((void *)&key, sizeof(jump_label_t), 0);
  60. head = &jump_label_table[hash & (JUMP_LABEL_TABLE_SIZE - 1)];
  61. hlist_for_each_entry(e, node, head, hlist) {
  62. if (key == e->key)
  63. return e;
  64. }
  65. return NULL;
  66. }
  67. static struct jump_label_entry *
  68. add_jump_label_entry(jump_label_t key, int nr_entries, struct jump_entry *table)
  69. {
  70. struct hlist_head *head;
  71. struct jump_label_entry *e;
  72. u32 hash;
  73. e = get_jump_label_entry(key);
  74. if (e)
  75. return ERR_PTR(-EEXIST);
  76. e = kmalloc(sizeof(struct jump_label_entry), GFP_KERNEL);
  77. if (!e)
  78. return ERR_PTR(-ENOMEM);
  79. hash = jhash((void *)&key, sizeof(jump_label_t), 0);
  80. head = &jump_label_table[hash & (JUMP_LABEL_TABLE_SIZE - 1)];
  81. e->key = key;
  82. e->table = table;
  83. e->nr_entries = nr_entries;
  84. INIT_HLIST_HEAD(&(e->modules));
  85. hlist_add_head(&e->hlist, head);
  86. return e;
  87. }
  88. static int
  89. build_jump_label_hashtable(struct jump_entry *start, struct jump_entry *stop)
  90. {
  91. struct jump_entry *iter, *iter_begin;
  92. struct jump_label_entry *entry;
  93. int count;
  94. sort_jump_label_entries(start, stop);
  95. iter = start;
  96. while (iter < stop) {
  97. entry = get_jump_label_entry(iter->key);
  98. if (!entry) {
  99. iter_begin = iter;
  100. count = 0;
  101. while ((iter < stop) &&
  102. (iter->key == iter_begin->key)) {
  103. iter++;
  104. count++;
  105. }
  106. entry = add_jump_label_entry(iter_begin->key,
  107. count, iter_begin);
  108. if (IS_ERR(entry))
  109. return PTR_ERR(entry);
  110. } else {
  111. WARN_ONCE(1, KERN_ERR "build_jump_hashtable: unexpected entry!\n");
  112. return -1;
  113. }
  114. }
  115. return 0;
  116. }
  117. /***
  118. * jump_label_update - update jump label text
  119. * @key - key value associated with a a jump label
  120. * @type - enum set to JUMP_LABEL_ENABLE or JUMP_LABEL_DISABLE
  121. *
  122. * Will enable/disable the jump for jump label @key, depending on the
  123. * value of @type.
  124. *
  125. */
  126. void jump_label_update(unsigned long key, enum jump_label_type type)
  127. {
  128. struct jump_entry *iter;
  129. struct jump_label_entry *entry;
  130. struct hlist_node *module_node;
  131. struct jump_label_module_entry *e_module;
  132. int count;
  133. mutex_lock(&jump_label_mutex);
  134. entry = get_jump_label_entry((jump_label_t)key);
  135. if (entry) {
  136. count = entry->nr_entries;
  137. iter = entry->table;
  138. while (count--) {
  139. if (kernel_text_address(iter->code))
  140. arch_jump_label_transform(iter, type);
  141. iter++;
  142. }
  143. /* eanble/disable jump labels in modules */
  144. hlist_for_each_entry(e_module, module_node, &(entry->modules),
  145. hlist) {
  146. count = e_module->nr_entries;
  147. iter = e_module->table;
  148. while (count--) {
  149. if (kernel_text_address(iter->code))
  150. arch_jump_label_transform(iter, type);
  151. iter++;
  152. }
  153. }
  154. }
  155. mutex_unlock(&jump_label_mutex);
  156. }
  157. static int addr_conflict(struct jump_entry *entry, void *start, void *end)
  158. {
  159. if (entry->code <= (unsigned long)end &&
  160. entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
  161. return 1;
  162. return 0;
  163. }
  164. #ifdef CONFIG_MODULES
  165. static int module_conflict(void *start, void *end)
  166. {
  167. struct hlist_head *head;
  168. struct hlist_node *node, *node_next, *module_node, *module_node_next;
  169. struct jump_label_entry *e;
  170. struct jump_label_module_entry *e_module;
  171. struct jump_entry *iter;
  172. int i, count;
  173. int conflict = 0;
  174. for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
  175. head = &jump_label_table[i];
  176. hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
  177. hlist_for_each_entry_safe(e_module, module_node,
  178. module_node_next,
  179. &(e->modules), hlist) {
  180. count = e_module->nr_entries;
  181. iter = e_module->table;
  182. while (count--) {
  183. if (addr_conflict(iter, start, end)) {
  184. conflict = 1;
  185. goto out;
  186. }
  187. iter++;
  188. }
  189. }
  190. }
  191. }
  192. out:
  193. return conflict;
  194. }
  195. #endif
  196. /***
  197. * jump_label_text_reserved - check if addr range is reserved
  198. * @start: start text addr
  199. * @end: end text addr
  200. *
  201. * checks if the text addr located between @start and @end
  202. * overlaps with any of the jump label patch addresses. Code
  203. * that wants to modify kernel text should first verify that
  204. * it does not overlap with any of the jump label addresses.
  205. *
  206. * returns 1 if there is an overlap, 0 otherwise
  207. */
  208. int jump_label_text_reserved(void *start, void *end)
  209. {
  210. struct jump_entry *iter;
  211. struct jump_entry *iter_start = __start___jump_table;
  212. struct jump_entry *iter_stop = __start___jump_table;
  213. int conflict = 0;
  214. mutex_lock(&jump_label_mutex);
  215. iter = iter_start;
  216. while (iter < iter_stop) {
  217. if (addr_conflict(iter, start, end)) {
  218. conflict = 1;
  219. goto out;
  220. }
  221. iter++;
  222. }
  223. /* now check modules */
  224. #ifdef CONFIG_MODULES
  225. conflict = module_conflict(start, end);
  226. #endif
  227. out:
  228. mutex_unlock(&jump_label_mutex);
  229. return conflict;
  230. }
  231. static __init int init_jump_label(void)
  232. {
  233. int ret;
  234. struct jump_entry *iter_start = __start___jump_table;
  235. struct jump_entry *iter_stop = __stop___jump_table;
  236. struct jump_entry *iter;
  237. mutex_lock(&jump_label_mutex);
  238. ret = build_jump_label_hashtable(__start___jump_table,
  239. __stop___jump_table);
  240. iter = iter_start;
  241. while (iter < iter_stop) {
  242. arch_jump_label_text_poke_early(iter->code);
  243. iter++;
  244. }
  245. mutex_unlock(&jump_label_mutex);
  246. return ret;
  247. }
  248. early_initcall(init_jump_label);
  249. #ifdef CONFIG_MODULES
  250. static struct jump_label_module_entry *
  251. add_jump_label_module_entry(struct jump_label_entry *entry,
  252. struct jump_entry *iter_begin,
  253. int count, struct module *mod)
  254. {
  255. struct jump_label_module_entry *e;
  256. e = kmalloc(sizeof(struct jump_label_module_entry), GFP_KERNEL);
  257. if (!e)
  258. return ERR_PTR(-ENOMEM);
  259. e->mod = mod;
  260. e->nr_entries = count;
  261. e->table = iter_begin;
  262. hlist_add_head(&e->hlist, &entry->modules);
  263. return e;
  264. }
  265. static int add_jump_label_module(struct module *mod)
  266. {
  267. struct jump_entry *iter, *iter_begin;
  268. struct jump_label_entry *entry;
  269. struct jump_label_module_entry *module_entry;
  270. int count;
  271. /* if the module doesn't have jump label entries, just return */
  272. if (!mod->num_jump_entries)
  273. return 0;
  274. sort_jump_label_entries(mod->jump_entries,
  275. mod->jump_entries + mod->num_jump_entries);
  276. iter = mod->jump_entries;
  277. while (iter < mod->jump_entries + mod->num_jump_entries) {
  278. entry = get_jump_label_entry(iter->key);
  279. iter_begin = iter;
  280. count = 0;
  281. while ((iter < mod->jump_entries + mod->num_jump_entries) &&
  282. (iter->key == iter_begin->key)) {
  283. iter++;
  284. count++;
  285. }
  286. if (!entry) {
  287. entry = add_jump_label_entry(iter_begin->key, 0, NULL);
  288. if (IS_ERR(entry))
  289. return PTR_ERR(entry);
  290. }
  291. module_entry = add_jump_label_module_entry(entry, iter_begin,
  292. count, mod);
  293. if (IS_ERR(module_entry))
  294. return PTR_ERR(module_entry);
  295. }
  296. return 0;
  297. }
  298. static void remove_jump_label_module(struct module *mod)
  299. {
  300. struct hlist_head *head;
  301. struct hlist_node *node, *node_next, *module_node, *module_node_next;
  302. struct jump_label_entry *e;
  303. struct jump_label_module_entry *e_module;
  304. int i;
  305. /* if the module doesn't have jump label entries, just return */
  306. if (!mod->num_jump_entries)
  307. return;
  308. for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
  309. head = &jump_label_table[i];
  310. hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
  311. hlist_for_each_entry_safe(e_module, module_node,
  312. module_node_next,
  313. &(e->modules), hlist) {
  314. if (e_module->mod == mod) {
  315. hlist_del(&e_module->hlist);
  316. kfree(e_module);
  317. }
  318. }
  319. if (hlist_empty(&e->modules) && (e->nr_entries == 0)) {
  320. hlist_del(&e->hlist);
  321. kfree(e);
  322. }
  323. }
  324. }
  325. }
  326. static int
  327. jump_label_module_notify(struct notifier_block *self, unsigned long val,
  328. void *data)
  329. {
  330. struct module *mod = data;
  331. int ret = 0;
  332. switch (val) {
  333. case MODULE_STATE_COMING:
  334. mutex_lock(&jump_label_mutex);
  335. ret = add_jump_label_module(mod);
  336. if (ret)
  337. remove_jump_label_module(mod);
  338. mutex_unlock(&jump_label_mutex);
  339. break;
  340. case MODULE_STATE_GOING:
  341. mutex_lock(&jump_label_mutex);
  342. remove_jump_label_module(mod);
  343. mutex_unlock(&jump_label_mutex);
  344. break;
  345. }
  346. return ret;
  347. }
  348. /***
  349. * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
  350. * @mod: module to patch
  351. *
  352. * Allow for run-time selection of the optimal nops. Before the module
  353. * loads patch these with arch_get_jump_label_nop(), which is specified by
  354. * the arch specific jump label code.
  355. */
  356. void jump_label_apply_nops(struct module *mod)
  357. {
  358. struct jump_entry *iter;
  359. /* if the module doesn't have jump label entries, just return */
  360. if (!mod->num_jump_entries)
  361. return;
  362. iter = mod->jump_entries;
  363. while (iter < mod->jump_entries + mod->num_jump_entries) {
  364. arch_jump_label_text_poke_early(iter->code);
  365. iter++;
  366. }
  367. }
  368. struct notifier_block jump_label_module_nb = {
  369. .notifier_call = jump_label_module_notify,
  370. .priority = 0,
  371. };
  372. static __init int init_jump_label_module(void)
  373. {
  374. return register_module_notifier(&jump_label_module_nb);
  375. }
  376. early_initcall(init_jump_label_module);
  377. #endif /* CONFIG_MODULES */
  378. #endif