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