jump_label.c 11 KB

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