jump_label.c 10 KB

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