jump_label.c 11 KB

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