jump_label.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 (iter->key &&
  150. kernel_text_address(iter->code))
  151. arch_jump_label_transform(iter, type);
  152. iter++;
  153. }
  154. }
  155. }
  156. mutex_unlock(&jump_label_mutex);
  157. }
  158. static int addr_conflict(struct jump_entry *entry, void *start, void *end)
  159. {
  160. if (entry->code <= (unsigned long)end &&
  161. entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
  162. return 1;
  163. return 0;
  164. }
  165. #ifdef CONFIG_MODULES
  166. static int module_conflict(void *start, void *end)
  167. {
  168. struct hlist_head *head;
  169. struct hlist_node *node, *node_next, *module_node, *module_node_next;
  170. struct jump_label_entry *e;
  171. struct jump_label_module_entry *e_module;
  172. struct jump_entry *iter;
  173. int i, count;
  174. int conflict = 0;
  175. for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
  176. head = &jump_label_table[i];
  177. hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
  178. hlist_for_each_entry_safe(e_module, module_node,
  179. module_node_next,
  180. &(e->modules), hlist) {
  181. count = e_module->nr_entries;
  182. iter = e_module->table;
  183. while (count--) {
  184. if (addr_conflict(iter, start, end)) {
  185. conflict = 1;
  186. goto out;
  187. }
  188. iter++;
  189. }
  190. }
  191. }
  192. }
  193. out:
  194. return conflict;
  195. }
  196. #endif
  197. /***
  198. * jump_label_text_reserved - check if addr range is reserved
  199. * @start: start text addr
  200. * @end: end text addr
  201. *
  202. * checks if the text addr located between @start and @end
  203. * overlaps with any of the jump label patch addresses. Code
  204. * that wants to modify kernel text should first verify that
  205. * it does not overlap with any of the jump label addresses.
  206. *
  207. * returns 1 if there is an overlap, 0 otherwise
  208. */
  209. int jump_label_text_reserved(void *start, void *end)
  210. {
  211. struct jump_entry *iter;
  212. struct jump_entry *iter_start = __start___jump_table;
  213. struct jump_entry *iter_stop = __start___jump_table;
  214. int conflict = 0;
  215. mutex_lock(&jump_label_mutex);
  216. iter = iter_start;
  217. while (iter < iter_stop) {
  218. if (addr_conflict(iter, start, end)) {
  219. conflict = 1;
  220. goto out;
  221. }
  222. iter++;
  223. }
  224. /* now check modules */
  225. #ifdef CONFIG_MODULES
  226. conflict = module_conflict(start, end);
  227. #endif
  228. out:
  229. mutex_unlock(&jump_label_mutex);
  230. return conflict;
  231. }
  232. static __init int init_jump_label(void)
  233. {
  234. int ret;
  235. struct jump_entry *iter_start = __start___jump_table;
  236. struct jump_entry *iter_stop = __stop___jump_table;
  237. struct jump_entry *iter;
  238. mutex_lock(&jump_label_mutex);
  239. ret = build_jump_label_hashtable(__start___jump_table,
  240. __stop___jump_table);
  241. iter = iter_start;
  242. while (iter < iter_stop) {
  243. arch_jump_label_text_poke_early(iter->code);
  244. iter++;
  245. }
  246. mutex_unlock(&jump_label_mutex);
  247. return ret;
  248. }
  249. early_initcall(init_jump_label);
  250. #ifdef CONFIG_MODULES
  251. static struct jump_label_module_entry *
  252. add_jump_label_module_entry(struct jump_label_entry *entry,
  253. struct jump_entry *iter_begin,
  254. int count, struct module *mod)
  255. {
  256. struct jump_label_module_entry *e;
  257. e = kmalloc(sizeof(struct jump_label_module_entry), GFP_KERNEL);
  258. if (!e)
  259. return ERR_PTR(-ENOMEM);
  260. e->mod = mod;
  261. e->nr_entries = count;
  262. e->table = iter_begin;
  263. hlist_add_head(&e->hlist, &entry->modules);
  264. return e;
  265. }
  266. static int add_jump_label_module(struct module *mod)
  267. {
  268. struct jump_entry *iter, *iter_begin;
  269. struct jump_label_entry *entry;
  270. struct jump_label_module_entry *module_entry;
  271. int count;
  272. /* if the module doesn't have jump label entries, just return */
  273. if (!mod->num_jump_entries)
  274. return 0;
  275. sort_jump_label_entries(mod->jump_entries,
  276. mod->jump_entries + mod->num_jump_entries);
  277. iter = mod->jump_entries;
  278. while (iter < mod->jump_entries + mod->num_jump_entries) {
  279. entry = get_jump_label_entry(iter->key);
  280. iter_begin = iter;
  281. count = 0;
  282. while ((iter < mod->jump_entries + mod->num_jump_entries) &&
  283. (iter->key == iter_begin->key)) {
  284. iter++;
  285. count++;
  286. }
  287. if (!entry) {
  288. entry = add_jump_label_entry(iter_begin->key, 0, NULL);
  289. if (IS_ERR(entry))
  290. return PTR_ERR(entry);
  291. }
  292. module_entry = add_jump_label_module_entry(entry, iter_begin,
  293. count, mod);
  294. if (IS_ERR(module_entry))
  295. return PTR_ERR(module_entry);
  296. }
  297. return 0;
  298. }
  299. static void remove_jump_label_module(struct module *mod)
  300. {
  301. struct hlist_head *head;
  302. struct hlist_node *node, *node_next, *module_node, *module_node_next;
  303. struct jump_label_entry *e;
  304. struct jump_label_module_entry *e_module;
  305. int i;
  306. /* if the module doesn't have jump label entries, just return */
  307. if (!mod->num_jump_entries)
  308. return;
  309. for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
  310. head = &jump_label_table[i];
  311. hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
  312. hlist_for_each_entry_safe(e_module, module_node,
  313. module_node_next,
  314. &(e->modules), hlist) {
  315. if (e_module->mod == mod) {
  316. hlist_del(&e_module->hlist);
  317. kfree(e_module);
  318. }
  319. }
  320. if (hlist_empty(&e->modules) && (e->nr_entries == 0)) {
  321. hlist_del(&e->hlist);
  322. kfree(e);
  323. }
  324. }
  325. }
  326. }
  327. static void remove_jump_label_module_init(struct module *mod)
  328. {
  329. struct hlist_head *head;
  330. struct hlist_node *node, *node_next, *module_node, *module_node_next;
  331. struct jump_label_entry *e;
  332. struct jump_label_module_entry *e_module;
  333. struct jump_entry *iter;
  334. int i, count;
  335. /* if the module doesn't have jump label entries, just return */
  336. if (!mod->num_jump_entries)
  337. return;
  338. for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
  339. head = &jump_label_table[i];
  340. hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
  341. hlist_for_each_entry_safe(e_module, module_node,
  342. module_node_next,
  343. &(e->modules), hlist) {
  344. if (e_module->mod != mod)
  345. continue;
  346. count = e_module->nr_entries;
  347. iter = e_module->table;
  348. while (count--) {
  349. if (within_module_init(iter->code, mod))
  350. iter->key = 0;
  351. iter++;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. static int
  358. jump_label_module_notify(struct notifier_block *self, unsigned long val,
  359. void *data)
  360. {
  361. struct module *mod = data;
  362. int ret = 0;
  363. switch (val) {
  364. case MODULE_STATE_COMING:
  365. mutex_lock(&jump_label_mutex);
  366. ret = add_jump_label_module(mod);
  367. if (ret)
  368. remove_jump_label_module(mod);
  369. mutex_unlock(&jump_label_mutex);
  370. break;
  371. case MODULE_STATE_GOING:
  372. mutex_lock(&jump_label_mutex);
  373. remove_jump_label_module(mod);
  374. mutex_unlock(&jump_label_mutex);
  375. break;
  376. case MODULE_STATE_LIVE:
  377. mutex_lock(&jump_label_mutex);
  378. remove_jump_label_module_init(mod);
  379. mutex_unlock(&jump_label_mutex);
  380. break;
  381. }
  382. return ret;
  383. }
  384. /***
  385. * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
  386. * @mod: module to patch
  387. *
  388. * Allow for run-time selection of the optimal nops. Before the module
  389. * loads patch these with arch_get_jump_label_nop(), which is specified by
  390. * the arch specific jump label code.
  391. */
  392. void jump_label_apply_nops(struct module *mod)
  393. {
  394. struct jump_entry *iter;
  395. /* if the module doesn't have jump label entries, just return */
  396. if (!mod->num_jump_entries)
  397. return;
  398. iter = mod->jump_entries;
  399. while (iter < mod->jump_entries + mod->num_jump_entries) {
  400. arch_jump_label_text_poke_early(iter->code);
  401. iter++;
  402. }
  403. }
  404. struct notifier_block jump_label_module_nb = {
  405. .notifier_call = jump_label_module_notify,
  406. .priority = 0,
  407. };
  408. static __init int init_jump_label_module(void)
  409. {
  410. return register_module_notifier(&jump_label_module_nb);
  411. }
  412. early_initcall(init_jump_label_module);
  413. #endif /* CONFIG_MODULES */
  414. #endif