jump_label.c 11 KB

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