jump_label.c 9.3 KB

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