jump_label.c 11 KB

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