jump_label.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * jump label support
  3. *
  4. * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
  5. * Copyright (C) 2011 Peter Zijlstra
  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. #include <linux/bug.h>
  18. #ifdef HAVE_JUMP_LABEL
  19. /* mutex to protect coming/going of the the jump_label table */
  20. static DEFINE_MUTEX(jump_label_mutex);
  21. void jump_label_lock(void)
  22. {
  23. mutex_lock(&jump_label_mutex);
  24. }
  25. void jump_label_unlock(void)
  26. {
  27. mutex_unlock(&jump_label_mutex);
  28. }
  29. static int jump_label_cmp(const void *a, const void *b)
  30. {
  31. const struct jump_entry *jea = a;
  32. const struct jump_entry *jeb = b;
  33. if (jea->key < jeb->key)
  34. return -1;
  35. if (jea->key > jeb->key)
  36. return 1;
  37. return 0;
  38. }
  39. static void
  40. jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
  41. {
  42. unsigned long size;
  43. size = (((unsigned long)stop - (unsigned long)start)
  44. / sizeof(struct jump_entry));
  45. sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
  46. }
  47. static void jump_label_update(struct static_key *key);
  48. /*
  49. * There are similar definitions for the !HAVE_JUMP_LABEL case in jump_label.h.
  50. * The use of 'atomic_read()' requires atomic.h and its problematic for some
  51. * kernel headers such as kernel.h and others. Since static_key_count() is not
  52. * used in the branch statements as it is for the !HAVE_JUMP_LABEL case its ok
  53. * to have it be a function here. Similarly, for 'static_key_enable()' and
  54. * 'static_key_disable()', which require bug.h. This should allow jump_label.h
  55. * to be included from most/all places for HAVE_JUMP_LABEL.
  56. */
  57. int static_key_count(struct static_key *key)
  58. {
  59. /*
  60. * -1 means the first static_key_slow_inc() is in progress.
  61. * static_key_enabled() must return true, so return 1 here.
  62. */
  63. int n = atomic_read(&key->enabled);
  64. return n >= 0 ? n : 1;
  65. }
  66. EXPORT_SYMBOL_GPL(static_key_count);
  67. void static_key_enable(struct static_key *key)
  68. {
  69. int count = static_key_count(key);
  70. WARN_ON_ONCE(count < 0 || count > 1);
  71. if (!count)
  72. static_key_slow_inc(key);
  73. }
  74. EXPORT_SYMBOL_GPL(static_key_enable);
  75. void static_key_disable(struct static_key *key)
  76. {
  77. int count = static_key_count(key);
  78. WARN_ON_ONCE(count < 0 || count > 1);
  79. if (count)
  80. static_key_slow_dec(key);
  81. }
  82. EXPORT_SYMBOL_GPL(static_key_disable);
  83. void static_key_slow_inc(struct static_key *key)
  84. {
  85. int v, v1;
  86. STATIC_KEY_CHECK_USE();
  87. /*
  88. * Careful if we get concurrent static_key_slow_inc() calls;
  89. * later calls must wait for the first one to _finish_ the
  90. * jump_label_update() process. At the same time, however,
  91. * the jump_label_update() call below wants to see
  92. * static_key_enabled(&key) for jumps to be updated properly.
  93. *
  94. * So give a special meaning to negative key->enabled: it sends
  95. * static_key_slow_inc() down the slow path, and it is non-zero
  96. * so it counts as "enabled" in jump_label_update(). Note that
  97. * atomic_inc_unless_negative() checks >= 0, so roll our own.
  98. */
  99. for (v = atomic_read(&key->enabled); v > 0; v = v1) {
  100. v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
  101. if (likely(v1 == v))
  102. return;
  103. }
  104. jump_label_lock();
  105. if (atomic_read(&key->enabled) == 0) {
  106. atomic_set(&key->enabled, -1);
  107. jump_label_update(key);
  108. atomic_set(&key->enabled, 1);
  109. } else {
  110. atomic_inc(&key->enabled);
  111. }
  112. jump_label_unlock();
  113. }
  114. EXPORT_SYMBOL_GPL(static_key_slow_inc);
  115. static void __static_key_slow_dec(struct static_key *key,
  116. unsigned long rate_limit, struct delayed_work *work)
  117. {
  118. /*
  119. * The negative count check is valid even when a negative
  120. * key->enabled is in use by static_key_slow_inc(); a
  121. * __static_key_slow_dec() before the first static_key_slow_inc()
  122. * returns is unbalanced, because all other static_key_slow_inc()
  123. * instances block while the update is in progress.
  124. */
  125. if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
  126. WARN(atomic_read(&key->enabled) < 0,
  127. "jump label: negative count!\n");
  128. return;
  129. }
  130. if (rate_limit) {
  131. atomic_inc(&key->enabled);
  132. schedule_delayed_work(work, rate_limit);
  133. } else {
  134. jump_label_update(key);
  135. }
  136. jump_label_unlock();
  137. }
  138. static void jump_label_update_timeout(struct work_struct *work)
  139. {
  140. struct static_key_deferred *key =
  141. container_of(work, struct static_key_deferred, work.work);
  142. __static_key_slow_dec(&key->key, 0, NULL);
  143. }
  144. void static_key_slow_dec(struct static_key *key)
  145. {
  146. STATIC_KEY_CHECK_USE();
  147. __static_key_slow_dec(key, 0, NULL);
  148. }
  149. EXPORT_SYMBOL_GPL(static_key_slow_dec);
  150. void static_key_slow_dec_deferred(struct static_key_deferred *key)
  151. {
  152. STATIC_KEY_CHECK_USE();
  153. __static_key_slow_dec(&key->key, key->timeout, &key->work);
  154. }
  155. EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
  156. void static_key_deferred_flush(struct static_key_deferred *key)
  157. {
  158. STATIC_KEY_CHECK_USE();
  159. flush_delayed_work(&key->work);
  160. }
  161. EXPORT_SYMBOL_GPL(static_key_deferred_flush);
  162. void jump_label_rate_limit(struct static_key_deferred *key,
  163. unsigned long rl)
  164. {
  165. STATIC_KEY_CHECK_USE();
  166. key->timeout = rl;
  167. INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
  168. }
  169. EXPORT_SYMBOL_GPL(jump_label_rate_limit);
  170. static int addr_conflict(struct jump_entry *entry, void *start, void *end)
  171. {
  172. if (entry->code <= (unsigned long)end &&
  173. entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
  174. return 1;
  175. return 0;
  176. }
  177. static int __jump_label_text_reserved(struct jump_entry *iter_start,
  178. struct jump_entry *iter_stop, void *start, void *end)
  179. {
  180. struct jump_entry *iter;
  181. iter = iter_start;
  182. while (iter < iter_stop) {
  183. if (addr_conflict(iter, start, end))
  184. return 1;
  185. iter++;
  186. }
  187. return 0;
  188. }
  189. /*
  190. * Update code which is definitely not currently executing.
  191. * Architectures which need heavyweight synchronization to modify
  192. * running code can override this to make the non-live update case
  193. * cheaper.
  194. */
  195. void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
  196. enum jump_label_type type)
  197. {
  198. arch_jump_label_transform(entry, type);
  199. }
  200. static inline struct jump_entry *static_key_entries(struct static_key *key)
  201. {
  202. return (struct jump_entry *)((unsigned long)key->entries & ~JUMP_TYPE_MASK);
  203. }
  204. static inline bool static_key_type(struct static_key *key)
  205. {
  206. return (unsigned long)key->entries & JUMP_TYPE_MASK;
  207. }
  208. static inline struct static_key *jump_entry_key(struct jump_entry *entry)
  209. {
  210. return (struct static_key *)((unsigned long)entry->key & ~1UL);
  211. }
  212. static bool jump_entry_branch(struct jump_entry *entry)
  213. {
  214. return (unsigned long)entry->key & 1UL;
  215. }
  216. static enum jump_label_type jump_label_type(struct jump_entry *entry)
  217. {
  218. struct static_key *key = jump_entry_key(entry);
  219. bool enabled = static_key_enabled(key);
  220. bool branch = jump_entry_branch(entry);
  221. /* See the comment in linux/jump_label.h */
  222. return enabled ^ branch;
  223. }
  224. static void __jump_label_update(struct static_key *key,
  225. struct jump_entry *entry,
  226. struct jump_entry *stop)
  227. {
  228. for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
  229. /*
  230. * entry->code set to 0 invalidates module init text sections
  231. * kernel_text_address() verifies we are not in core kernel
  232. * init code, see jump_label_invalidate_module_init().
  233. */
  234. if (entry->code && kernel_text_address(entry->code))
  235. arch_jump_label_transform(entry, jump_label_type(entry));
  236. }
  237. }
  238. void __init jump_label_init(void)
  239. {
  240. struct jump_entry *iter_start = __start___jump_table;
  241. struct jump_entry *iter_stop = __stop___jump_table;
  242. struct static_key *key = NULL;
  243. struct jump_entry *iter;
  244. /*
  245. * Since we are initializing the static_key.enabled field with
  246. * with the 'raw' int values (to avoid pulling in atomic.h) in
  247. * jump_label.h, let's make sure that is safe. There are only two
  248. * cases to check since we initialize to 0 or 1.
  249. */
  250. BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
  251. BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
  252. if (static_key_initialized)
  253. return;
  254. jump_label_lock();
  255. jump_label_sort_entries(iter_start, iter_stop);
  256. for (iter = iter_start; iter < iter_stop; iter++) {
  257. struct static_key *iterk;
  258. /* rewrite NOPs */
  259. if (jump_label_type(iter) == JUMP_LABEL_NOP)
  260. arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
  261. iterk = jump_entry_key(iter);
  262. if (iterk == key)
  263. continue;
  264. key = iterk;
  265. /*
  266. * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
  267. */
  268. *((unsigned long *)&key->entries) += (unsigned long)iter;
  269. #ifdef CONFIG_MODULES
  270. key->next = NULL;
  271. #endif
  272. }
  273. static_key_initialized = true;
  274. jump_label_unlock();
  275. }
  276. #ifdef CONFIG_MODULES
  277. static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
  278. {
  279. struct static_key *key = jump_entry_key(entry);
  280. bool type = static_key_type(key);
  281. bool branch = jump_entry_branch(entry);
  282. /* See the comment in linux/jump_label.h */
  283. return type ^ branch;
  284. }
  285. struct static_key_mod {
  286. struct static_key_mod *next;
  287. struct jump_entry *entries;
  288. struct module *mod;
  289. };
  290. static int __jump_label_mod_text_reserved(void *start, void *end)
  291. {
  292. struct module *mod;
  293. preempt_disable();
  294. mod = __module_text_address((unsigned long)start);
  295. WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
  296. preempt_enable();
  297. if (!mod)
  298. return 0;
  299. return __jump_label_text_reserved(mod->jump_entries,
  300. mod->jump_entries + mod->num_jump_entries,
  301. start, end);
  302. }
  303. static void __jump_label_mod_update(struct static_key *key)
  304. {
  305. struct static_key_mod *mod;
  306. for (mod = key->next; mod; mod = mod->next) {
  307. struct module *m = mod->mod;
  308. __jump_label_update(key, mod->entries,
  309. m->jump_entries + m->num_jump_entries);
  310. }
  311. }
  312. /***
  313. * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
  314. * @mod: module to patch
  315. *
  316. * Allow for run-time selection of the optimal nops. Before the module
  317. * loads patch these with arch_get_jump_label_nop(), which is specified by
  318. * the arch specific jump label code.
  319. */
  320. void jump_label_apply_nops(struct module *mod)
  321. {
  322. struct jump_entry *iter_start = mod->jump_entries;
  323. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  324. struct jump_entry *iter;
  325. /* if the module doesn't have jump label entries, just return */
  326. if (iter_start == iter_stop)
  327. return;
  328. for (iter = iter_start; iter < iter_stop; iter++) {
  329. /* Only write NOPs for arch_branch_static(). */
  330. if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
  331. arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
  332. }
  333. }
  334. static int jump_label_add_module(struct module *mod)
  335. {
  336. struct jump_entry *iter_start = mod->jump_entries;
  337. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  338. struct jump_entry *iter;
  339. struct static_key *key = NULL;
  340. struct static_key_mod *jlm;
  341. /* if the module doesn't have jump label entries, just return */
  342. if (iter_start == iter_stop)
  343. return 0;
  344. jump_label_sort_entries(iter_start, iter_stop);
  345. for (iter = iter_start; iter < iter_stop; iter++) {
  346. struct static_key *iterk;
  347. iterk = jump_entry_key(iter);
  348. if (iterk == key)
  349. continue;
  350. key = iterk;
  351. if (within_module(iter->key, mod)) {
  352. /*
  353. * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
  354. */
  355. *((unsigned long *)&key->entries) += (unsigned long)iter;
  356. key->next = NULL;
  357. continue;
  358. }
  359. jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
  360. if (!jlm)
  361. return -ENOMEM;
  362. jlm->mod = mod;
  363. jlm->entries = iter;
  364. jlm->next = key->next;
  365. key->next = jlm;
  366. /* Only update if we've changed from our initial state */
  367. if (jump_label_type(iter) != jump_label_init_type(iter))
  368. __jump_label_update(key, iter, iter_stop);
  369. }
  370. return 0;
  371. }
  372. static void jump_label_del_module(struct module *mod)
  373. {
  374. struct jump_entry *iter_start = mod->jump_entries;
  375. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  376. struct jump_entry *iter;
  377. struct static_key *key = NULL;
  378. struct static_key_mod *jlm, **prev;
  379. for (iter = iter_start; iter < iter_stop; iter++) {
  380. if (jump_entry_key(iter) == key)
  381. continue;
  382. key = jump_entry_key(iter);
  383. if (within_module(iter->key, mod))
  384. continue;
  385. prev = &key->next;
  386. jlm = key->next;
  387. while (jlm && jlm->mod != mod) {
  388. prev = &jlm->next;
  389. jlm = jlm->next;
  390. }
  391. if (jlm) {
  392. *prev = jlm->next;
  393. kfree(jlm);
  394. }
  395. }
  396. }
  397. static void jump_label_invalidate_module_init(struct module *mod)
  398. {
  399. struct jump_entry *iter_start = mod->jump_entries;
  400. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  401. struct jump_entry *iter;
  402. for (iter = iter_start; iter < iter_stop; iter++) {
  403. if (within_module_init(iter->code, mod))
  404. iter->code = 0;
  405. }
  406. }
  407. static int
  408. jump_label_module_notify(struct notifier_block *self, unsigned long val,
  409. void *data)
  410. {
  411. struct module *mod = data;
  412. int ret = 0;
  413. switch (val) {
  414. case MODULE_STATE_COMING:
  415. jump_label_lock();
  416. ret = jump_label_add_module(mod);
  417. if (ret)
  418. jump_label_del_module(mod);
  419. jump_label_unlock();
  420. break;
  421. case MODULE_STATE_GOING:
  422. jump_label_lock();
  423. jump_label_del_module(mod);
  424. jump_label_unlock();
  425. break;
  426. case MODULE_STATE_LIVE:
  427. jump_label_lock();
  428. jump_label_invalidate_module_init(mod);
  429. jump_label_unlock();
  430. break;
  431. }
  432. return notifier_from_errno(ret);
  433. }
  434. static struct notifier_block jump_label_module_nb = {
  435. .notifier_call = jump_label_module_notify,
  436. .priority = 1, /* higher than tracepoints */
  437. };
  438. static __init int jump_label_init_module(void)
  439. {
  440. return register_module_notifier(&jump_label_module_nb);
  441. }
  442. early_initcall(jump_label_init_module);
  443. #endif /* CONFIG_MODULES */
  444. /***
  445. * jump_label_text_reserved - check if addr range is reserved
  446. * @start: start text addr
  447. * @end: end text addr
  448. *
  449. * checks if the text addr located between @start and @end
  450. * overlaps with any of the jump label patch addresses. Code
  451. * that wants to modify kernel text should first verify that
  452. * it does not overlap with any of the jump label addresses.
  453. * Caller must hold jump_label_mutex.
  454. *
  455. * returns 1 if there is an overlap, 0 otherwise
  456. */
  457. int jump_label_text_reserved(void *start, void *end)
  458. {
  459. int ret = __jump_label_text_reserved(__start___jump_table,
  460. __stop___jump_table, start, end);
  461. if (ret)
  462. return ret;
  463. #ifdef CONFIG_MODULES
  464. ret = __jump_label_mod_text_reserved(start, end);
  465. #endif
  466. return ret;
  467. }
  468. static void jump_label_update(struct static_key *key)
  469. {
  470. struct jump_entry *stop = __stop___jump_table;
  471. struct jump_entry *entry = static_key_entries(key);
  472. #ifdef CONFIG_MODULES
  473. struct module *mod;
  474. __jump_label_mod_update(key);
  475. preempt_disable();
  476. mod = __module_address((unsigned long)key);
  477. if (mod)
  478. stop = mod->jump_entries + mod->num_jump_entries;
  479. preempt_enable();
  480. #endif
  481. /* if there are no users, entry can be NULL */
  482. if (entry)
  483. __jump_label_update(key, entry, stop);
  484. }
  485. #ifdef CONFIG_STATIC_KEYS_SELFTEST
  486. static DEFINE_STATIC_KEY_TRUE(sk_true);
  487. static DEFINE_STATIC_KEY_FALSE(sk_false);
  488. static __init int jump_label_test(void)
  489. {
  490. int i;
  491. for (i = 0; i < 2; i++) {
  492. WARN_ON(static_key_enabled(&sk_true.key) != true);
  493. WARN_ON(static_key_enabled(&sk_false.key) != false);
  494. WARN_ON(!static_branch_likely(&sk_true));
  495. WARN_ON(!static_branch_unlikely(&sk_true));
  496. WARN_ON(static_branch_likely(&sk_false));
  497. WARN_ON(static_branch_unlikely(&sk_false));
  498. static_branch_disable(&sk_true);
  499. static_branch_enable(&sk_false);
  500. WARN_ON(static_key_enabled(&sk_true.key) == true);
  501. WARN_ON(static_key_enabled(&sk_false.key) == false);
  502. WARN_ON(static_branch_likely(&sk_true));
  503. WARN_ON(static_branch_unlikely(&sk_true));
  504. WARN_ON(!static_branch_likely(&sk_false));
  505. WARN_ON(!static_branch_unlikely(&sk_false));
  506. static_branch_enable(&sk_true);
  507. static_branch_disable(&sk_false);
  508. }
  509. return 0;
  510. }
  511. early_initcall(jump_label_test);
  512. #endif /* STATIC_KEYS_SELFTEST */
  513. #endif /* HAVE_JUMP_LABEL */