jump_label.c 13 KB

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