jump_label.c 9.0 KB

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