jump_label.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /*
  240. * Not all archs need this.
  241. */
  242. void __weak arch_jump_label_text_poke_early(jump_label_t addr)
  243. {
  244. }
  245. static __init int init_jump_label(void)
  246. {
  247. int ret;
  248. struct jump_entry *iter_start = __start___jump_table;
  249. struct jump_entry *iter_stop = __stop___jump_table;
  250. struct jump_entry *iter;
  251. jump_label_lock();
  252. ret = build_jump_label_hashtable(__start___jump_table,
  253. __stop___jump_table);
  254. iter = iter_start;
  255. while (iter < iter_stop) {
  256. arch_jump_label_text_poke_early(iter->code);
  257. iter++;
  258. }
  259. jump_label_unlock();
  260. return ret;
  261. }
  262. early_initcall(init_jump_label);
  263. #ifdef CONFIG_MODULES
  264. static struct jump_label_module_entry *
  265. add_jump_label_module_entry(struct jump_label_entry *entry,
  266. struct jump_entry *iter_begin,
  267. int count, struct module *mod)
  268. {
  269. struct jump_label_module_entry *e;
  270. e = kmalloc(sizeof(struct jump_label_module_entry), GFP_KERNEL);
  271. if (!e)
  272. return ERR_PTR(-ENOMEM);
  273. e->mod = mod;
  274. e->nr_entries = count;
  275. e->table = iter_begin;
  276. hlist_add_head(&e->hlist, &entry->modules);
  277. return e;
  278. }
  279. static int add_jump_label_module(struct module *mod)
  280. {
  281. struct jump_entry *iter, *iter_begin;
  282. struct jump_label_entry *entry;
  283. struct jump_label_module_entry *module_entry;
  284. int count;
  285. /* if the module doesn't have jump label entries, just return */
  286. if (!mod->num_jump_entries)
  287. return 0;
  288. sort_jump_label_entries(mod->jump_entries,
  289. mod->jump_entries + mod->num_jump_entries);
  290. iter = mod->jump_entries;
  291. while (iter < mod->jump_entries + mod->num_jump_entries) {
  292. entry = get_jump_label_entry(iter->key);
  293. iter_begin = iter;
  294. count = 0;
  295. while ((iter < mod->jump_entries + mod->num_jump_entries) &&
  296. (iter->key == iter_begin->key)) {
  297. iter++;
  298. count++;
  299. }
  300. if (!entry) {
  301. entry = add_jump_label_entry(iter_begin->key, 0, NULL);
  302. if (IS_ERR(entry))
  303. return PTR_ERR(entry);
  304. }
  305. module_entry = add_jump_label_module_entry(entry, iter_begin,
  306. count, mod);
  307. if (IS_ERR(module_entry))
  308. return PTR_ERR(module_entry);
  309. }
  310. return 0;
  311. }
  312. static void remove_jump_label_module(struct module *mod)
  313. {
  314. struct hlist_head *head;
  315. struct hlist_node *node, *node_next, *module_node, *module_node_next;
  316. struct jump_label_entry *e;
  317. struct jump_label_module_entry *e_module;
  318. int i;
  319. /* if the module doesn't have jump label entries, just return */
  320. if (!mod->num_jump_entries)
  321. return;
  322. for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
  323. head = &jump_label_table[i];
  324. hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
  325. hlist_for_each_entry_safe(e_module, module_node,
  326. module_node_next,
  327. &(e->modules), hlist) {
  328. if (e_module->mod == mod) {
  329. hlist_del(&e_module->hlist);
  330. kfree(e_module);
  331. }
  332. }
  333. if (hlist_empty(&e->modules) && (e->nr_entries == 0)) {
  334. hlist_del(&e->hlist);
  335. kfree(e);
  336. }
  337. }
  338. }
  339. }
  340. static void remove_jump_label_module_init(struct module *mod)
  341. {
  342. struct hlist_head *head;
  343. struct hlist_node *node, *node_next, *module_node, *module_node_next;
  344. struct jump_label_entry *e;
  345. struct jump_label_module_entry *e_module;
  346. struct jump_entry *iter;
  347. int i, count;
  348. /* if the module doesn't have jump label entries, just return */
  349. if (!mod->num_jump_entries)
  350. return;
  351. for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
  352. head = &jump_label_table[i];
  353. hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
  354. hlist_for_each_entry_safe(e_module, module_node,
  355. module_node_next,
  356. &(e->modules), hlist) {
  357. if (e_module->mod != mod)
  358. continue;
  359. count = e_module->nr_entries;
  360. iter = e_module->table;
  361. while (count--) {
  362. if (within_module_init(iter->code, mod))
  363. iter->key = 0;
  364. iter++;
  365. }
  366. }
  367. }
  368. }
  369. }
  370. static int
  371. jump_label_module_notify(struct notifier_block *self, unsigned long val,
  372. void *data)
  373. {
  374. struct module *mod = data;
  375. int ret = 0;
  376. switch (val) {
  377. case MODULE_STATE_COMING:
  378. jump_label_lock();
  379. ret = add_jump_label_module(mod);
  380. if (ret)
  381. remove_jump_label_module(mod);
  382. jump_label_unlock();
  383. break;
  384. case MODULE_STATE_GOING:
  385. jump_label_lock();
  386. remove_jump_label_module(mod);
  387. jump_label_unlock();
  388. break;
  389. case MODULE_STATE_LIVE:
  390. jump_label_lock();
  391. remove_jump_label_module_init(mod);
  392. jump_label_unlock();
  393. break;
  394. }
  395. return ret;
  396. }
  397. /***
  398. * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
  399. * @mod: module to patch
  400. *
  401. * Allow for run-time selection of the optimal nops. Before the module
  402. * loads patch these with arch_get_jump_label_nop(), which is specified by
  403. * the arch specific jump label code.
  404. */
  405. void jump_label_apply_nops(struct module *mod)
  406. {
  407. struct jump_entry *iter;
  408. /* if the module doesn't have jump label entries, just return */
  409. if (!mod->num_jump_entries)
  410. return;
  411. iter = mod->jump_entries;
  412. while (iter < mod->jump_entries + mod->num_jump_entries) {
  413. arch_jump_label_text_poke_early(iter->code);
  414. iter++;
  415. }
  416. }
  417. struct notifier_block jump_label_module_nb = {
  418. .notifier_call = jump_label_module_notify,
  419. .priority = 0,
  420. };
  421. static __init int init_jump_label_module(void)
  422. {
  423. return register_module_notifier(&jump_label_module_nb);
  424. }
  425. early_initcall(init_jump_label_module);
  426. #endif /* CONFIG_MODULES */
  427. #endif