jump_label.c 11 KB

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