jump_label.c 14 KB

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