jump_label.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. #include <linux/cpu.h>
  19. #include <asm/sections.h>
  20. #ifdef HAVE_JUMP_LABEL
  21. /* mutex to protect coming/going of the the jump_label table */
  22. static DEFINE_MUTEX(jump_label_mutex);
  23. void jump_label_lock(void)
  24. {
  25. mutex_lock(&jump_label_mutex);
  26. }
  27. void jump_label_unlock(void)
  28. {
  29. mutex_unlock(&jump_label_mutex);
  30. }
  31. static int jump_label_cmp(const void *a, const void *b)
  32. {
  33. const struct jump_entry *jea = a;
  34. const struct jump_entry *jeb = b;
  35. if (jea->key < jeb->key)
  36. return -1;
  37. if (jea->key > jeb->key)
  38. return 1;
  39. return 0;
  40. }
  41. static void
  42. jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
  43. {
  44. unsigned long size;
  45. size = (((unsigned long)stop - (unsigned long)start)
  46. / sizeof(struct jump_entry));
  47. sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
  48. }
  49. static void jump_label_update(struct static_key *key);
  50. /*
  51. * There are similar definitions for the !HAVE_JUMP_LABEL case in jump_label.h.
  52. * The use of 'atomic_read()' requires atomic.h and its problematic for some
  53. * kernel headers such as kernel.h and others. Since static_key_count() is not
  54. * used in the branch statements as it is for the !HAVE_JUMP_LABEL case its ok
  55. * to have it be a function here. Similarly, for 'static_key_enable()' and
  56. * 'static_key_disable()', which require bug.h. This should allow jump_label.h
  57. * to be included from most/all places for HAVE_JUMP_LABEL.
  58. */
  59. int static_key_count(struct static_key *key)
  60. {
  61. /*
  62. * -1 means the first static_key_slow_inc() is in progress.
  63. * static_key_enabled() must return true, so return 1 here.
  64. */
  65. int n = atomic_read(&key->enabled);
  66. return n >= 0 ? n : 1;
  67. }
  68. EXPORT_SYMBOL_GPL(static_key_count);
  69. void static_key_slow_inc_cpuslocked(struct static_key *key)
  70. {
  71. int v, v1;
  72. STATIC_KEY_CHECK_USE(key);
  73. /*
  74. * Careful if we get concurrent static_key_slow_inc() calls;
  75. * later calls must wait for the first one to _finish_ the
  76. * jump_label_update() process. At the same time, however,
  77. * the jump_label_update() call below wants to see
  78. * static_key_enabled(&key) for jumps to be updated properly.
  79. *
  80. * So give a special meaning to negative key->enabled: it sends
  81. * static_key_slow_inc() down the slow path, and it is non-zero
  82. * so it counts as "enabled" in jump_label_update(). Note that
  83. * atomic_inc_unless_negative() checks >= 0, so roll our own.
  84. */
  85. for (v = atomic_read(&key->enabled); v > 0; v = v1) {
  86. v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
  87. if (likely(v1 == v))
  88. return;
  89. }
  90. jump_label_lock();
  91. if (atomic_read(&key->enabled) == 0) {
  92. atomic_set(&key->enabled, -1);
  93. jump_label_update(key);
  94. /*
  95. * Ensure that if the above cmpxchg loop observes our positive
  96. * value, it must also observe all the text changes.
  97. */
  98. atomic_set_release(&key->enabled, 1);
  99. } else {
  100. atomic_inc(&key->enabled);
  101. }
  102. jump_label_unlock();
  103. }
  104. void static_key_slow_inc(struct static_key *key)
  105. {
  106. cpus_read_lock();
  107. static_key_slow_inc_cpuslocked(key);
  108. cpus_read_unlock();
  109. }
  110. EXPORT_SYMBOL_GPL(static_key_slow_inc);
  111. void static_key_enable_cpuslocked(struct static_key *key)
  112. {
  113. STATIC_KEY_CHECK_USE(key);
  114. if (atomic_read(&key->enabled) > 0) {
  115. WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
  116. return;
  117. }
  118. jump_label_lock();
  119. if (atomic_read(&key->enabled) == 0) {
  120. atomic_set(&key->enabled, -1);
  121. jump_label_update(key);
  122. /*
  123. * See static_key_slow_inc().
  124. */
  125. atomic_set_release(&key->enabled, 1);
  126. }
  127. jump_label_unlock();
  128. }
  129. EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
  130. void static_key_enable(struct static_key *key)
  131. {
  132. cpus_read_lock();
  133. static_key_enable_cpuslocked(key);
  134. cpus_read_unlock();
  135. }
  136. EXPORT_SYMBOL_GPL(static_key_enable);
  137. void static_key_disable_cpuslocked(struct static_key *key)
  138. {
  139. STATIC_KEY_CHECK_USE(key);
  140. if (atomic_read(&key->enabled) != 1) {
  141. WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
  142. return;
  143. }
  144. jump_label_lock();
  145. if (atomic_cmpxchg(&key->enabled, 1, 0))
  146. jump_label_update(key);
  147. jump_label_unlock();
  148. }
  149. EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
  150. void static_key_disable(struct static_key *key)
  151. {
  152. cpus_read_lock();
  153. static_key_disable_cpuslocked(key);
  154. cpus_read_unlock();
  155. }
  156. EXPORT_SYMBOL_GPL(static_key_disable);
  157. static void __static_key_slow_dec_cpuslocked(struct static_key *key,
  158. unsigned long rate_limit,
  159. struct delayed_work *work)
  160. {
  161. /*
  162. * The negative count check is valid even when a negative
  163. * key->enabled is in use by static_key_slow_inc(); a
  164. * __static_key_slow_dec() before the first static_key_slow_inc()
  165. * returns is unbalanced, because all other static_key_slow_inc()
  166. * instances block while the update is in progress.
  167. */
  168. if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
  169. WARN(atomic_read(&key->enabled) < 0,
  170. "jump label: negative count!\n");
  171. return;
  172. }
  173. if (rate_limit) {
  174. atomic_inc(&key->enabled);
  175. schedule_delayed_work(work, rate_limit);
  176. } else {
  177. jump_label_update(key);
  178. }
  179. jump_label_unlock();
  180. }
  181. static void __static_key_slow_dec(struct static_key *key,
  182. unsigned long rate_limit,
  183. struct delayed_work *work)
  184. {
  185. cpus_read_lock();
  186. __static_key_slow_dec_cpuslocked(key, rate_limit, work);
  187. cpus_read_unlock();
  188. }
  189. static void jump_label_update_timeout(struct work_struct *work)
  190. {
  191. struct static_key_deferred *key =
  192. container_of(work, struct static_key_deferred, work.work);
  193. __static_key_slow_dec(&key->key, 0, NULL);
  194. }
  195. void static_key_slow_dec(struct static_key *key)
  196. {
  197. STATIC_KEY_CHECK_USE(key);
  198. __static_key_slow_dec(key, 0, NULL);
  199. }
  200. EXPORT_SYMBOL_GPL(static_key_slow_dec);
  201. void static_key_slow_dec_cpuslocked(struct static_key *key)
  202. {
  203. STATIC_KEY_CHECK_USE(key);
  204. __static_key_slow_dec_cpuslocked(key, 0, NULL);
  205. }
  206. void static_key_slow_dec_deferred(struct static_key_deferred *key)
  207. {
  208. STATIC_KEY_CHECK_USE(key);
  209. __static_key_slow_dec(&key->key, key->timeout, &key->work);
  210. }
  211. EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
  212. void static_key_deferred_flush(struct static_key_deferred *key)
  213. {
  214. STATIC_KEY_CHECK_USE(key);
  215. flush_delayed_work(&key->work);
  216. }
  217. EXPORT_SYMBOL_GPL(static_key_deferred_flush);
  218. void jump_label_rate_limit(struct static_key_deferred *key,
  219. unsigned long rl)
  220. {
  221. STATIC_KEY_CHECK_USE(key);
  222. key->timeout = rl;
  223. INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
  224. }
  225. EXPORT_SYMBOL_GPL(jump_label_rate_limit);
  226. static int addr_conflict(struct jump_entry *entry, void *start, void *end)
  227. {
  228. if (entry->code <= (unsigned long)end &&
  229. entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
  230. return 1;
  231. return 0;
  232. }
  233. static int __jump_label_text_reserved(struct jump_entry *iter_start,
  234. struct jump_entry *iter_stop, void *start, void *end)
  235. {
  236. struct jump_entry *iter;
  237. iter = iter_start;
  238. while (iter < iter_stop) {
  239. if (addr_conflict(iter, start, end))
  240. return 1;
  241. iter++;
  242. }
  243. return 0;
  244. }
  245. /*
  246. * Update code which is definitely not currently executing.
  247. * Architectures which need heavyweight synchronization to modify
  248. * running code can override this to make the non-live update case
  249. * cheaper.
  250. */
  251. void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
  252. enum jump_label_type type)
  253. {
  254. arch_jump_label_transform(entry, type);
  255. }
  256. static inline struct jump_entry *static_key_entries(struct static_key *key)
  257. {
  258. WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
  259. return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
  260. }
  261. static inline bool static_key_type(struct static_key *key)
  262. {
  263. return key->type & JUMP_TYPE_TRUE;
  264. }
  265. static inline bool static_key_linked(struct static_key *key)
  266. {
  267. return key->type & JUMP_TYPE_LINKED;
  268. }
  269. static inline void static_key_clear_linked(struct static_key *key)
  270. {
  271. key->type &= ~JUMP_TYPE_LINKED;
  272. }
  273. static inline void static_key_set_linked(struct static_key *key)
  274. {
  275. key->type |= JUMP_TYPE_LINKED;
  276. }
  277. static inline struct static_key *jump_entry_key(struct jump_entry *entry)
  278. {
  279. return (struct static_key *)((unsigned long)entry->key & ~1UL);
  280. }
  281. static bool jump_entry_branch(struct jump_entry *entry)
  282. {
  283. return (unsigned long)entry->key & 1UL;
  284. }
  285. /***
  286. * A 'struct static_key' uses a union such that it either points directly
  287. * to a table of 'struct jump_entry' or to a linked list of modules which in
  288. * turn point to 'struct jump_entry' tables.
  289. *
  290. * The two lower bits of the pointer are used to keep track of which pointer
  291. * type is in use and to store the initial branch direction, we use an access
  292. * function which preserves these bits.
  293. */
  294. static void static_key_set_entries(struct static_key *key,
  295. struct jump_entry *entries)
  296. {
  297. unsigned long type;
  298. WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
  299. type = key->type & JUMP_TYPE_MASK;
  300. key->entries = entries;
  301. key->type |= type;
  302. }
  303. static enum jump_label_type jump_label_type(struct jump_entry *entry)
  304. {
  305. struct static_key *key = jump_entry_key(entry);
  306. bool enabled = static_key_enabled(key);
  307. bool branch = jump_entry_branch(entry);
  308. /* See the comment in linux/jump_label.h */
  309. return enabled ^ branch;
  310. }
  311. static void __jump_label_update(struct static_key *key,
  312. struct jump_entry *entry,
  313. struct jump_entry *stop)
  314. {
  315. for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
  316. /*
  317. * An entry->code of 0 indicates an entry which has been
  318. * disabled because it was in an init text area.
  319. */
  320. if (entry->code) {
  321. if (kernel_text_address(entry->code))
  322. arch_jump_label_transform(entry, jump_label_type(entry));
  323. else
  324. WARN_ONCE(1, "can't patch jump_label at %pS",
  325. (void *)(unsigned long)entry->code);
  326. }
  327. }
  328. }
  329. void __init jump_label_init(void)
  330. {
  331. struct jump_entry *iter_start = __start___jump_table;
  332. struct jump_entry *iter_stop = __stop___jump_table;
  333. struct static_key *key = NULL;
  334. struct jump_entry *iter;
  335. /*
  336. * Since we are initializing the static_key.enabled field with
  337. * with the 'raw' int values (to avoid pulling in atomic.h) in
  338. * jump_label.h, let's make sure that is safe. There are only two
  339. * cases to check since we initialize to 0 or 1.
  340. */
  341. BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
  342. BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
  343. if (static_key_initialized)
  344. return;
  345. cpus_read_lock();
  346. jump_label_lock();
  347. jump_label_sort_entries(iter_start, iter_stop);
  348. for (iter = iter_start; iter < iter_stop; iter++) {
  349. struct static_key *iterk;
  350. /* rewrite NOPs */
  351. if (jump_label_type(iter) == JUMP_LABEL_NOP)
  352. arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
  353. iterk = jump_entry_key(iter);
  354. if (iterk == key)
  355. continue;
  356. key = iterk;
  357. static_key_set_entries(key, iter);
  358. }
  359. static_key_initialized = true;
  360. jump_label_unlock();
  361. cpus_read_unlock();
  362. }
  363. /* Disable any jump label entries in __init/__exit code */
  364. void __init jump_label_invalidate_initmem(void)
  365. {
  366. struct jump_entry *iter_start = __start___jump_table;
  367. struct jump_entry *iter_stop = __stop___jump_table;
  368. struct jump_entry *iter;
  369. for (iter = iter_start; iter < iter_stop; iter++) {
  370. if (init_section_contains((void *)(unsigned long)iter->code, 1))
  371. iter->code = 0;
  372. }
  373. }
  374. #ifdef CONFIG_MODULES
  375. static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
  376. {
  377. struct static_key *key = jump_entry_key(entry);
  378. bool type = static_key_type(key);
  379. bool branch = jump_entry_branch(entry);
  380. /* See the comment in linux/jump_label.h */
  381. return type ^ branch;
  382. }
  383. struct static_key_mod {
  384. struct static_key_mod *next;
  385. struct jump_entry *entries;
  386. struct module *mod;
  387. };
  388. static inline struct static_key_mod *static_key_mod(struct static_key *key)
  389. {
  390. WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
  391. return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
  392. }
  393. /***
  394. * key->type and key->next are the same via union.
  395. * This sets key->next and preserves the type bits.
  396. *
  397. * See additional comments above static_key_set_entries().
  398. */
  399. static void static_key_set_mod(struct static_key *key,
  400. struct static_key_mod *mod)
  401. {
  402. unsigned long type;
  403. WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
  404. type = key->type & JUMP_TYPE_MASK;
  405. key->next = mod;
  406. key->type |= type;
  407. }
  408. static int __jump_label_mod_text_reserved(void *start, void *end)
  409. {
  410. struct module *mod;
  411. preempt_disable();
  412. mod = __module_text_address((unsigned long)start);
  413. WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
  414. preempt_enable();
  415. if (!mod)
  416. return 0;
  417. return __jump_label_text_reserved(mod->jump_entries,
  418. mod->jump_entries + mod->num_jump_entries,
  419. start, end);
  420. }
  421. static void __jump_label_mod_update(struct static_key *key)
  422. {
  423. struct static_key_mod *mod;
  424. for (mod = static_key_mod(key); mod; mod = mod->next) {
  425. struct jump_entry *stop;
  426. struct module *m;
  427. /*
  428. * NULL if the static_key is defined in a module
  429. * that does not use it
  430. */
  431. if (!mod->entries)
  432. continue;
  433. m = mod->mod;
  434. if (!m)
  435. stop = __stop___jump_table;
  436. else
  437. stop = m->jump_entries + m->num_jump_entries;
  438. __jump_label_update(key, mod->entries, stop);
  439. }
  440. }
  441. /***
  442. * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
  443. * @mod: module to patch
  444. *
  445. * Allow for run-time selection of the optimal nops. Before the module
  446. * loads patch these with arch_get_jump_label_nop(), which is specified by
  447. * the arch specific jump label code.
  448. */
  449. void jump_label_apply_nops(struct module *mod)
  450. {
  451. struct jump_entry *iter_start = mod->jump_entries;
  452. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  453. struct jump_entry *iter;
  454. /* if the module doesn't have jump label entries, just return */
  455. if (iter_start == iter_stop)
  456. return;
  457. for (iter = iter_start; iter < iter_stop; iter++) {
  458. /* Only write NOPs for arch_branch_static(). */
  459. if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
  460. arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
  461. }
  462. }
  463. static int jump_label_add_module(struct module *mod)
  464. {
  465. struct jump_entry *iter_start = mod->jump_entries;
  466. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  467. struct jump_entry *iter;
  468. struct static_key *key = NULL;
  469. struct static_key_mod *jlm, *jlm2;
  470. /* if the module doesn't have jump label entries, just return */
  471. if (iter_start == iter_stop)
  472. return 0;
  473. jump_label_sort_entries(iter_start, iter_stop);
  474. for (iter = iter_start; iter < iter_stop; iter++) {
  475. struct static_key *iterk;
  476. iterk = jump_entry_key(iter);
  477. if (iterk == key)
  478. continue;
  479. key = iterk;
  480. if (within_module(iter->key, mod)) {
  481. static_key_set_entries(key, iter);
  482. continue;
  483. }
  484. jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
  485. if (!jlm)
  486. return -ENOMEM;
  487. if (!static_key_linked(key)) {
  488. jlm2 = kzalloc(sizeof(struct static_key_mod),
  489. GFP_KERNEL);
  490. if (!jlm2) {
  491. kfree(jlm);
  492. return -ENOMEM;
  493. }
  494. preempt_disable();
  495. jlm2->mod = __module_address((unsigned long)key);
  496. preempt_enable();
  497. jlm2->entries = static_key_entries(key);
  498. jlm2->next = NULL;
  499. static_key_set_mod(key, jlm2);
  500. static_key_set_linked(key);
  501. }
  502. jlm->mod = mod;
  503. jlm->entries = iter;
  504. jlm->next = static_key_mod(key);
  505. static_key_set_mod(key, jlm);
  506. static_key_set_linked(key);
  507. /* Only update if we've changed from our initial state */
  508. if (jump_label_type(iter) != jump_label_init_type(iter))
  509. __jump_label_update(key, iter, iter_stop);
  510. }
  511. return 0;
  512. }
  513. static void jump_label_del_module(struct module *mod)
  514. {
  515. struct jump_entry *iter_start = mod->jump_entries;
  516. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  517. struct jump_entry *iter;
  518. struct static_key *key = NULL;
  519. struct static_key_mod *jlm, **prev;
  520. for (iter = iter_start; iter < iter_stop; iter++) {
  521. if (jump_entry_key(iter) == key)
  522. continue;
  523. key = jump_entry_key(iter);
  524. if (within_module(iter->key, mod))
  525. continue;
  526. /* No memory during module load */
  527. if (WARN_ON(!static_key_linked(key)))
  528. continue;
  529. prev = &key->next;
  530. jlm = static_key_mod(key);
  531. while (jlm && jlm->mod != mod) {
  532. prev = &jlm->next;
  533. jlm = jlm->next;
  534. }
  535. /* No memory during module load */
  536. if (WARN_ON(!jlm))
  537. continue;
  538. if (prev == &key->next)
  539. static_key_set_mod(key, jlm->next);
  540. else
  541. *prev = jlm->next;
  542. kfree(jlm);
  543. jlm = static_key_mod(key);
  544. /* if only one etry is left, fold it back into the static_key */
  545. if (jlm->next == NULL) {
  546. static_key_set_entries(key, jlm->entries);
  547. static_key_clear_linked(key);
  548. kfree(jlm);
  549. }
  550. }
  551. }
  552. /* Disable any jump label entries in module init code */
  553. static void jump_label_invalidate_module_init(struct module *mod)
  554. {
  555. struct jump_entry *iter_start = mod->jump_entries;
  556. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  557. struct jump_entry *iter;
  558. for (iter = iter_start; iter < iter_stop; iter++) {
  559. if (within_module_init(iter->code, mod))
  560. iter->code = 0;
  561. }
  562. }
  563. static int
  564. jump_label_module_notify(struct notifier_block *self, unsigned long val,
  565. void *data)
  566. {
  567. struct module *mod = data;
  568. int ret = 0;
  569. cpus_read_lock();
  570. jump_label_lock();
  571. switch (val) {
  572. case MODULE_STATE_COMING:
  573. ret = jump_label_add_module(mod);
  574. if (ret) {
  575. WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
  576. jump_label_del_module(mod);
  577. }
  578. break;
  579. case MODULE_STATE_GOING:
  580. jump_label_del_module(mod);
  581. break;
  582. case MODULE_STATE_LIVE:
  583. jump_label_invalidate_module_init(mod);
  584. break;
  585. }
  586. jump_label_unlock();
  587. cpus_read_unlock();
  588. return notifier_from_errno(ret);
  589. }
  590. static struct notifier_block jump_label_module_nb = {
  591. .notifier_call = jump_label_module_notify,
  592. .priority = 1, /* higher than tracepoints */
  593. };
  594. static __init int jump_label_init_module(void)
  595. {
  596. return register_module_notifier(&jump_label_module_nb);
  597. }
  598. early_initcall(jump_label_init_module);
  599. #endif /* CONFIG_MODULES */
  600. /***
  601. * jump_label_text_reserved - check if addr range is reserved
  602. * @start: start text addr
  603. * @end: end text addr
  604. *
  605. * checks if the text addr located between @start and @end
  606. * overlaps with any of the jump label patch addresses. Code
  607. * that wants to modify kernel text should first verify that
  608. * it does not overlap with any of the jump label addresses.
  609. * Caller must hold jump_label_mutex.
  610. *
  611. * returns 1 if there is an overlap, 0 otherwise
  612. */
  613. int jump_label_text_reserved(void *start, void *end)
  614. {
  615. int ret = __jump_label_text_reserved(__start___jump_table,
  616. __stop___jump_table, start, end);
  617. if (ret)
  618. return ret;
  619. #ifdef CONFIG_MODULES
  620. ret = __jump_label_mod_text_reserved(start, end);
  621. #endif
  622. return ret;
  623. }
  624. static void jump_label_update(struct static_key *key)
  625. {
  626. struct jump_entry *stop = __stop___jump_table;
  627. struct jump_entry *entry;
  628. #ifdef CONFIG_MODULES
  629. struct module *mod;
  630. if (static_key_linked(key)) {
  631. __jump_label_mod_update(key);
  632. return;
  633. }
  634. preempt_disable();
  635. mod = __module_address((unsigned long)key);
  636. if (mod)
  637. stop = mod->jump_entries + mod->num_jump_entries;
  638. preempt_enable();
  639. #endif
  640. entry = static_key_entries(key);
  641. /* if there are no users, entry can be NULL */
  642. if (entry)
  643. __jump_label_update(key, entry, stop);
  644. }
  645. #ifdef CONFIG_STATIC_KEYS_SELFTEST
  646. static DEFINE_STATIC_KEY_TRUE(sk_true);
  647. static DEFINE_STATIC_KEY_FALSE(sk_false);
  648. static __init int jump_label_test(void)
  649. {
  650. int i;
  651. for (i = 0; i < 2; i++) {
  652. WARN_ON(static_key_enabled(&sk_true.key) != true);
  653. WARN_ON(static_key_enabled(&sk_false.key) != false);
  654. WARN_ON(!static_branch_likely(&sk_true));
  655. WARN_ON(!static_branch_unlikely(&sk_true));
  656. WARN_ON(static_branch_likely(&sk_false));
  657. WARN_ON(static_branch_unlikely(&sk_false));
  658. static_branch_disable(&sk_true);
  659. static_branch_enable(&sk_false);
  660. WARN_ON(static_key_enabled(&sk_true.key) == true);
  661. WARN_ON(static_key_enabled(&sk_false.key) == false);
  662. WARN_ON(static_branch_likely(&sk_true));
  663. WARN_ON(static_branch_unlikely(&sk_true));
  664. WARN_ON(!static_branch_likely(&sk_false));
  665. WARN_ON(!static_branch_unlikely(&sk_false));
  666. static_branch_enable(&sk_true);
  667. static_branch_disable(&sk_false);
  668. }
  669. return 0;
  670. }
  671. early_initcall(jump_label_test);
  672. #endif /* STATIC_KEYS_SELFTEST */
  673. #endif /* HAVE_JUMP_LABEL */