jump_label.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * jump label support
  3. *
  4. * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
  5. * Copyright (C) 2011 Peter Zijlstra <pzijlstr@redhat.com>
  6. *
  7. */
  8. #include <linux/memory.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/module.h>
  11. #include <linux/list.h>
  12. #include <linux/slab.h>
  13. #include <linux/sort.h>
  14. #include <linux/err.h>
  15. #include <linux/jump_label.h>
  16. #ifdef HAVE_JUMP_LABEL
  17. /* mutex to protect coming/going of the the jump_label table */
  18. static DEFINE_MUTEX(jump_label_mutex);
  19. void jump_label_lock(void)
  20. {
  21. mutex_lock(&jump_label_mutex);
  22. }
  23. void jump_label_unlock(void)
  24. {
  25. mutex_unlock(&jump_label_mutex);
  26. }
  27. bool jump_label_enabled(struct jump_label_key *key)
  28. {
  29. return !!atomic_read(&key->enabled);
  30. }
  31. static int jump_label_cmp(const void *a, const void *b)
  32. {
  33. const struct jump_entry *jea = a;
  34. const struct jump_entry *jeb = b;
  35. if (jea->key < jeb->key)
  36. return -1;
  37. if (jea->key > jeb->key)
  38. return 1;
  39. return 0;
  40. }
  41. static void
  42. jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
  43. {
  44. unsigned long size;
  45. size = (((unsigned long)stop - (unsigned long)start)
  46. / sizeof(struct jump_entry));
  47. sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
  48. }
  49. static void jump_label_update(struct jump_label_key *key, int enable);
  50. void jump_label_inc(struct jump_label_key *key)
  51. {
  52. if (atomic_inc_not_zero(&key->enabled))
  53. return;
  54. jump_label_lock();
  55. if (atomic_add_return(1, &key->enabled) == 1)
  56. jump_label_update(key, JUMP_LABEL_ENABLE);
  57. jump_label_unlock();
  58. }
  59. void jump_label_dec(struct jump_label_key *key)
  60. {
  61. if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex))
  62. return;
  63. jump_label_update(key, JUMP_LABEL_DISABLE);
  64. jump_label_unlock();
  65. }
  66. static int addr_conflict(struct jump_entry *entry, void *start, void *end)
  67. {
  68. if (entry->code <= (unsigned long)end &&
  69. entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
  70. return 1;
  71. return 0;
  72. }
  73. static int __jump_label_text_reserved(struct jump_entry *iter_start,
  74. struct jump_entry *iter_stop, void *start, void *end)
  75. {
  76. struct jump_entry *iter;
  77. iter = iter_start;
  78. while (iter < iter_stop) {
  79. if (addr_conflict(iter, start, end))
  80. return 1;
  81. iter++;
  82. }
  83. return 0;
  84. }
  85. static void __jump_label_update(struct jump_label_key *key,
  86. struct jump_entry *entry,
  87. struct jump_entry *stop, int enable)
  88. {
  89. for (; (entry < stop) &&
  90. (entry->key == (jump_label_t)(unsigned long)key);
  91. entry++) {
  92. /*
  93. * entry->code set to 0 invalidates module init text sections
  94. * kernel_text_address() verifies we are not in core kernel
  95. * init code, see jump_label_invalidate_module_init().
  96. */
  97. if (entry->code && kernel_text_address(entry->code))
  98. arch_jump_label_transform(entry, enable);
  99. }
  100. }
  101. /*
  102. * Not all archs need this.
  103. */
  104. void __weak arch_jump_label_text_poke_early(jump_label_t addr)
  105. {
  106. }
  107. static __init int jump_label_init(void)
  108. {
  109. struct jump_entry *iter_start = __start___jump_table;
  110. struct jump_entry *iter_stop = __stop___jump_table;
  111. struct jump_label_key *key = NULL;
  112. struct jump_entry *iter;
  113. jump_label_lock();
  114. jump_label_sort_entries(iter_start, iter_stop);
  115. for (iter = iter_start; iter < iter_stop; iter++) {
  116. arch_jump_label_text_poke_early(iter->code);
  117. if (iter->key == (jump_label_t)(unsigned long)key)
  118. continue;
  119. key = (struct jump_label_key *)(unsigned long)iter->key;
  120. atomic_set(&key->enabled, 0);
  121. key->entries = iter;
  122. #ifdef CONFIG_MODULES
  123. key->next = NULL;
  124. #endif
  125. }
  126. jump_label_unlock();
  127. return 0;
  128. }
  129. early_initcall(jump_label_init);
  130. #ifdef CONFIG_MODULES
  131. struct jump_label_mod {
  132. struct jump_label_mod *next;
  133. struct jump_entry *entries;
  134. struct module *mod;
  135. };
  136. static int __jump_label_mod_text_reserved(void *start, void *end)
  137. {
  138. struct module *mod;
  139. mod = __module_text_address((unsigned long)start);
  140. if (!mod)
  141. return 0;
  142. WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
  143. return __jump_label_text_reserved(mod->jump_entries,
  144. mod->jump_entries + mod->num_jump_entries,
  145. start, end);
  146. }
  147. static void __jump_label_mod_update(struct jump_label_key *key, int enable)
  148. {
  149. struct jump_label_mod *mod = key->next;
  150. while (mod) {
  151. struct module *m = mod->mod;
  152. __jump_label_update(key, mod->entries,
  153. m->jump_entries + m->num_jump_entries,
  154. enable);
  155. mod = mod->next;
  156. }
  157. }
  158. /***
  159. * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
  160. * @mod: module to patch
  161. *
  162. * Allow for run-time selection of the optimal nops. Before the module
  163. * loads patch these with arch_get_jump_label_nop(), which is specified by
  164. * the arch specific jump label code.
  165. */
  166. void jump_label_apply_nops(struct module *mod)
  167. {
  168. struct jump_entry *iter_start = mod->jump_entries;
  169. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  170. struct jump_entry *iter;
  171. /* if the module doesn't have jump label entries, just return */
  172. if (iter_start == iter_stop)
  173. return;
  174. for (iter = iter_start; iter < iter_stop; iter++)
  175. arch_jump_label_text_poke_early(iter->code);
  176. }
  177. static int jump_label_add_module(struct module *mod)
  178. {
  179. struct jump_entry *iter_start = mod->jump_entries;
  180. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  181. struct jump_entry *iter;
  182. struct jump_label_key *key = NULL;
  183. struct jump_label_mod *jlm;
  184. /* if the module doesn't have jump label entries, just return */
  185. if (iter_start == iter_stop)
  186. return 0;
  187. jump_label_sort_entries(iter_start, iter_stop);
  188. for (iter = iter_start; iter < iter_stop; iter++) {
  189. if (iter->key == (jump_label_t)(unsigned long)key)
  190. continue;
  191. key = (struct jump_label_key *)(unsigned long)iter->key;
  192. if (__module_address(iter->key) == mod) {
  193. atomic_set(&key->enabled, 0);
  194. key->entries = iter;
  195. key->next = NULL;
  196. continue;
  197. }
  198. jlm = kzalloc(sizeof(struct jump_label_mod), GFP_KERNEL);
  199. if (!jlm)
  200. return -ENOMEM;
  201. jlm->mod = mod;
  202. jlm->entries = iter;
  203. jlm->next = key->next;
  204. key->next = jlm;
  205. if (jump_label_enabled(key))
  206. __jump_label_update(key, iter, iter_stop,
  207. JUMP_LABEL_ENABLE);
  208. }
  209. return 0;
  210. }
  211. static void jump_label_del_module(struct module *mod)
  212. {
  213. struct jump_entry *iter_start = mod->jump_entries;
  214. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  215. struct jump_entry *iter;
  216. struct jump_label_key *key = NULL;
  217. struct jump_label_mod *jlm, **prev;
  218. for (iter = iter_start; iter < iter_stop; iter++) {
  219. if (iter->key == (jump_label_t)(unsigned long)key)
  220. continue;
  221. key = (struct jump_label_key *)(unsigned long)iter->key;
  222. if (__module_address(iter->key) == mod)
  223. continue;
  224. prev = &key->next;
  225. jlm = key->next;
  226. while (jlm && jlm->mod != mod) {
  227. prev = &jlm->next;
  228. jlm = jlm->next;
  229. }
  230. if (jlm) {
  231. *prev = jlm->next;
  232. kfree(jlm);
  233. }
  234. }
  235. }
  236. static void jump_label_invalidate_module_init(struct module *mod)
  237. {
  238. struct jump_entry *iter_start = mod->jump_entries;
  239. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  240. struct jump_entry *iter;
  241. for (iter = iter_start; iter < iter_stop; iter++) {
  242. if (within_module_init(iter->code, mod))
  243. iter->code = 0;
  244. }
  245. }
  246. static int
  247. jump_label_module_notify(struct notifier_block *self, unsigned long val,
  248. void *data)
  249. {
  250. struct module *mod = data;
  251. int ret = 0;
  252. switch (val) {
  253. case MODULE_STATE_COMING:
  254. jump_label_lock();
  255. ret = jump_label_add_module(mod);
  256. if (ret)
  257. jump_label_del_module(mod);
  258. jump_label_unlock();
  259. break;
  260. case MODULE_STATE_GOING:
  261. jump_label_lock();
  262. jump_label_del_module(mod);
  263. jump_label_unlock();
  264. break;
  265. case MODULE_STATE_LIVE:
  266. jump_label_lock();
  267. jump_label_invalidate_module_init(mod);
  268. jump_label_unlock();
  269. break;
  270. }
  271. return notifier_from_errno(ret);
  272. }
  273. struct notifier_block jump_label_module_nb = {
  274. .notifier_call = jump_label_module_notify,
  275. .priority = 1, /* higher than tracepoints */
  276. };
  277. static __init int jump_label_init_module(void)
  278. {
  279. return register_module_notifier(&jump_label_module_nb);
  280. }
  281. early_initcall(jump_label_init_module);
  282. #endif /* CONFIG_MODULES */
  283. /***
  284. * jump_label_text_reserved - check if addr range is reserved
  285. * @start: start text addr
  286. * @end: end text addr
  287. *
  288. * checks if the text addr located between @start and @end
  289. * overlaps with any of the jump label patch addresses. Code
  290. * that wants to modify kernel text should first verify that
  291. * it does not overlap with any of the jump label addresses.
  292. * Caller must hold jump_label_mutex.
  293. *
  294. * returns 1 if there is an overlap, 0 otherwise
  295. */
  296. int jump_label_text_reserved(void *start, void *end)
  297. {
  298. int ret = __jump_label_text_reserved(__start___jump_table,
  299. __stop___jump_table, start, end);
  300. if (ret)
  301. return ret;
  302. #ifdef CONFIG_MODULES
  303. ret = __jump_label_mod_text_reserved(start, end);
  304. #endif
  305. return ret;
  306. }
  307. static void jump_label_update(struct jump_label_key *key, int enable)
  308. {
  309. struct jump_entry *entry = key->entries, *stop = __stop___jump_table;
  310. #ifdef CONFIG_MODULES
  311. struct module *mod = __module_address((jump_label_t)key);
  312. __jump_label_mod_update(key, enable);
  313. if (mod)
  314. stop = mod->jump_entries + mod->num_jump_entries;
  315. #endif
  316. /* if there are no users, entry can be NULL */
  317. if (entry)
  318. __jump_label_update(key, entry, stop, enable);
  319. }
  320. #endif