jump_label.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * jump label support
  4. *
  5. * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
  6. * Copyright (C) 2011 Peter Zijlstra
  7. *
  8. */
  9. #include <linux/memory.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/module.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/sort.h>
  15. #include <linux/err.h>
  16. #include <linux/static_key.h>
  17. #include <linux/jump_label_ratelimit.h>
  18. #include <linux/bug.h>
  19. #include <linux/cpu.h>
  20. #include <asm/sections.h>
  21. /* mutex to protect coming/going of 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. /*
  36. * Entrires are sorted by key.
  37. */
  38. if (jump_entry_key(jea) < jump_entry_key(jeb))
  39. return -1;
  40. if (jump_entry_key(jea) > jump_entry_key(jeb))
  41. return 1;
  42. /*
  43. * In the batching mode, entries should also be sorted by the code
  44. * inside the already sorted list of entries, enabling a bsearch in
  45. * the vector.
  46. */
  47. if (jump_entry_code(jea) < jump_entry_code(jeb))
  48. return -1;
  49. if (jump_entry_code(jea) > jump_entry_code(jeb))
  50. return 1;
  51. return 0;
  52. }
  53. static void jump_label_swap(void *a, void *b, int size)
  54. {
  55. long delta = (unsigned long)a - (unsigned long)b;
  56. struct jump_entry *jea = a;
  57. struct jump_entry *jeb = b;
  58. struct jump_entry tmp = *jea;
  59. jea->code = jeb->code - delta;
  60. jea->target = jeb->target - delta;
  61. jea->key = jeb->key - delta;
  62. jeb->code = tmp.code + delta;
  63. jeb->target = tmp.target + delta;
  64. jeb->key = tmp.key + delta;
  65. }
  66. static void
  67. jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
  68. {
  69. unsigned long size;
  70. void *swapfn = NULL;
  71. if (IS_ENABLED(CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE))
  72. swapfn = jump_label_swap;
  73. size = (((unsigned long)stop - (unsigned long)start)
  74. / sizeof(struct jump_entry));
  75. sort(start, size, sizeof(struct jump_entry), jump_label_cmp, swapfn);
  76. }
  77. static void jump_label_update(struct static_key *key);
  78. /*
  79. * There are similar definitions for the !CONFIG_JUMP_LABEL case in jump_label.h.
  80. * The use of 'atomic_read()' requires atomic.h and its problematic for some
  81. * kernel headers such as kernel.h and others. Since static_key_count() is not
  82. * used in the branch statements as it is for the !CONFIG_JUMP_LABEL case its ok
  83. * to have it be a function here. Similarly, for 'static_key_enable()' and
  84. * 'static_key_disable()', which require bug.h. This should allow jump_label.h
  85. * to be included from most/all places for CONFIG_JUMP_LABEL.
  86. */
  87. int static_key_count(struct static_key *key)
  88. {
  89. /*
  90. * -1 means the first static_key_slow_inc() is in progress.
  91. * static_key_enabled() must return true, so return 1 here.
  92. */
  93. int n = atomic_read(&key->enabled);
  94. return n >= 0 ? n : 1;
  95. }
  96. EXPORT_SYMBOL_GPL(static_key_count);
  97. /*
  98. * static_key_fast_inc_not_disabled - adds a user for a static key
  99. * @key: static key that must be already enabled
  100. *
  101. * The caller must make sure that the static key can't get disabled while
  102. * in this function. It doesn't patch jump labels, only adds a user to
  103. * an already enabled static key.
  104. *
  105. * Returns true if the increment was done. Unlike refcount_t the ref counter
  106. * is not saturated, but will fail to increment on overflow.
  107. */
  108. bool static_key_fast_inc_not_disabled(struct static_key *key)
  109. {
  110. int v;
  111. STATIC_KEY_CHECK_USE(key);
  112. /*
  113. * Negative key->enabled has a special meaning: it sends
  114. * static_key_slow_inc/dec() down the slow path, and it is non-zero
  115. * so it counts as "enabled" in jump_label_update().
  116. *
  117. * The INT_MAX overflow condition is either used by the networking
  118. * code to reset or detected in the slow path of
  119. * static_key_slow_inc_cpuslocked().
  120. */
  121. v = atomic_read(&key->enabled);
  122. do {
  123. if (v <= 0 || v == INT_MAX)
  124. return false;
  125. } while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1)));
  126. return true;
  127. }
  128. EXPORT_SYMBOL_GPL(static_key_fast_inc_not_disabled);
  129. bool static_key_slow_inc_cpuslocked(struct static_key *key)
  130. {
  131. lockdep_assert_cpus_held();
  132. /*
  133. * Careful if we get concurrent static_key_slow_inc/dec() calls;
  134. * later calls must wait for the first one to _finish_ the
  135. * jump_label_update() process. At the same time, however,
  136. * the jump_label_update() call below wants to see
  137. * static_key_enabled(&key) for jumps to be updated properly.
  138. */
  139. if (static_key_fast_inc_not_disabled(key))
  140. return true;
  141. guard(mutex)(&jump_label_mutex);
  142. /* Try to mark it as 'enabling in progress. */
  143. if (!atomic_cmpxchg(&key->enabled, 0, -1)) {
  144. jump_label_update(key);
  145. /*
  146. * Ensure that when static_key_fast_inc_not_disabled() or
  147. * static_key_dec_not_one() observe the positive value,
  148. * they must also observe all the text changes.
  149. */
  150. atomic_set_release(&key->enabled, 1);
  151. } else {
  152. /*
  153. * While holding the mutex this should never observe
  154. * anything else than a value >= 1 and succeed
  155. */
  156. if (WARN_ON_ONCE(!static_key_fast_inc_not_disabled(key)))
  157. return false;
  158. }
  159. return true;
  160. }
  161. bool static_key_slow_inc(struct static_key *key)
  162. {
  163. bool ret;
  164. cpus_read_lock();
  165. ret = static_key_slow_inc_cpuslocked(key);
  166. cpus_read_unlock();
  167. return ret;
  168. }
  169. EXPORT_SYMBOL_GPL(static_key_slow_inc);
  170. void static_key_enable_cpuslocked(struct static_key *key)
  171. {
  172. STATIC_KEY_CHECK_USE(key);
  173. lockdep_assert_cpus_held();
  174. if (atomic_read(&key->enabled) > 0) {
  175. WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
  176. return;
  177. }
  178. jump_label_lock();
  179. if (atomic_read(&key->enabled) == 0) {
  180. atomic_set(&key->enabled, -1);
  181. jump_label_update(key);
  182. /*
  183. * See static_key_slow_inc().
  184. */
  185. atomic_set_release(&key->enabled, 1);
  186. }
  187. jump_label_unlock();
  188. }
  189. EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
  190. void static_key_enable(struct static_key *key)
  191. {
  192. cpus_read_lock();
  193. static_key_enable_cpuslocked(key);
  194. cpus_read_unlock();
  195. }
  196. EXPORT_SYMBOL_GPL(static_key_enable);
  197. void static_key_disable_cpuslocked(struct static_key *key)
  198. {
  199. STATIC_KEY_CHECK_USE(key);
  200. lockdep_assert_cpus_held();
  201. if (atomic_read(&key->enabled) != 1) {
  202. WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
  203. return;
  204. }
  205. jump_label_lock();
  206. if (atomic_cmpxchg(&key->enabled, 1, 0) == 1)
  207. jump_label_update(key);
  208. jump_label_unlock();
  209. }
  210. EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
  211. void static_key_disable(struct static_key *key)
  212. {
  213. cpus_read_lock();
  214. static_key_disable_cpuslocked(key);
  215. cpus_read_unlock();
  216. }
  217. EXPORT_SYMBOL_GPL(static_key_disable);
  218. static bool static_key_dec_not_one(struct static_key *key)
  219. {
  220. int v;
  221. /*
  222. * Go into the slow path if key::enabled is less than or equal than
  223. * one. One is valid to shut down the key, anything less than one
  224. * is an imbalance, which is handled at the call site.
  225. *
  226. * That includes the special case of '-1' which is set in
  227. * static_key_slow_inc_cpuslocked(), but that's harmless as it is
  228. * fully serialized in the slow path below. By the time this task
  229. * acquires the jump label lock the value is back to one and the
  230. * retry under the lock must succeed.
  231. */
  232. v = atomic_read(&key->enabled);
  233. do {
  234. /*
  235. * Warn about the '-1' case though; since that means a
  236. * decrement is concurrent with a first (0->1) increment. IOW
  237. * people are trying to disable something that wasn't yet fully
  238. * enabled. This suggests an ordering problem on the user side.
  239. */
  240. WARN_ON_ONCE(v < 0);
  241. /*
  242. * Warn about underflow, and lie about success in an attempt to
  243. * not make things worse.
  244. */
  245. if (WARN_ON_ONCE(v == 0))
  246. return true;
  247. if (v <= 1)
  248. return false;
  249. } while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v - 1)));
  250. return true;
  251. }
  252. static void __static_key_slow_dec_cpuslocked(struct static_key *key)
  253. {
  254. lockdep_assert_cpus_held();
  255. int val;
  256. if (static_key_dec_not_one(key))
  257. return;
  258. guard(mutex)(&jump_label_mutex);
  259. val = atomic_read(&key->enabled);
  260. /*
  261. * It should be impossible to observe -1 with jump_label_mutex held,
  262. * see static_key_slow_inc_cpuslocked().
  263. */
  264. if (WARN_ON_ONCE(val == -1))
  265. return;
  266. /*
  267. * Cannot already be 0, something went sideways.
  268. */
  269. if (WARN_ON_ONCE(val == 0))
  270. return;
  271. if (atomic_dec_and_test(&key->enabled))
  272. jump_label_update(key);
  273. }
  274. static void __static_key_slow_dec(struct static_key *key)
  275. {
  276. cpus_read_lock();
  277. __static_key_slow_dec_cpuslocked(key);
  278. cpus_read_unlock();
  279. }
  280. void jump_label_update_timeout(struct work_struct *work)
  281. {
  282. struct static_key_deferred *key =
  283. container_of(work, struct static_key_deferred, work.work);
  284. __static_key_slow_dec(&key->key);
  285. }
  286. EXPORT_SYMBOL_GPL(jump_label_update_timeout);
  287. void static_key_slow_dec(struct static_key *key)
  288. {
  289. STATIC_KEY_CHECK_USE(key);
  290. __static_key_slow_dec(key);
  291. }
  292. EXPORT_SYMBOL_GPL(static_key_slow_dec);
  293. void static_key_slow_dec_cpuslocked(struct static_key *key)
  294. {
  295. STATIC_KEY_CHECK_USE(key);
  296. __static_key_slow_dec_cpuslocked(key);
  297. }
  298. void __static_key_slow_dec_deferred(struct static_key *key,
  299. struct delayed_work *work,
  300. unsigned long timeout)
  301. {
  302. STATIC_KEY_CHECK_USE(key);
  303. if (static_key_dec_not_one(key))
  304. return;
  305. schedule_delayed_work(work, timeout);
  306. }
  307. EXPORT_SYMBOL_GPL(__static_key_slow_dec_deferred);
  308. void __static_key_deferred_flush(void *key, struct delayed_work *work)
  309. {
  310. STATIC_KEY_CHECK_USE(key);
  311. flush_delayed_work(work);
  312. }
  313. EXPORT_SYMBOL_GPL(__static_key_deferred_flush);
  314. void jump_label_rate_limit(struct static_key_deferred *key,
  315. unsigned long rl)
  316. {
  317. STATIC_KEY_CHECK_USE(key);
  318. key->timeout = rl;
  319. INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
  320. }
  321. EXPORT_SYMBOL_GPL(jump_label_rate_limit);
  322. static int addr_conflict(struct jump_entry *entry, void *start, void *end)
  323. {
  324. if (jump_entry_code(entry) <= (unsigned long)end &&
  325. jump_entry_code(entry) + jump_entry_size(entry) > (unsigned long)start)
  326. return 1;
  327. return 0;
  328. }
  329. static int __jump_label_text_reserved(struct jump_entry *iter_start,
  330. struct jump_entry *iter_stop, void *start, void *end, bool init)
  331. {
  332. struct jump_entry *iter;
  333. iter = iter_start;
  334. while (iter < iter_stop) {
  335. if (init || !jump_entry_is_init(iter)) {
  336. if (addr_conflict(iter, start, end))
  337. return 1;
  338. }
  339. iter++;
  340. }
  341. return 0;
  342. }
  343. #ifndef arch_jump_label_transform_static
  344. static void arch_jump_label_transform_static(struct jump_entry *entry,
  345. enum jump_label_type type)
  346. {
  347. /* nothing to do on most architectures */
  348. }
  349. #endif
  350. static inline struct jump_entry *static_key_entries(struct static_key *key)
  351. {
  352. WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
  353. return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
  354. }
  355. static inline bool static_key_type(struct static_key *key)
  356. {
  357. return key->type & JUMP_TYPE_TRUE;
  358. }
  359. static inline bool static_key_linked(struct static_key *key)
  360. {
  361. return key->type & JUMP_TYPE_LINKED;
  362. }
  363. static inline void static_key_clear_linked(struct static_key *key)
  364. {
  365. key->type &= ~JUMP_TYPE_LINKED;
  366. }
  367. static inline void static_key_set_linked(struct static_key *key)
  368. {
  369. key->type |= JUMP_TYPE_LINKED;
  370. }
  371. /***
  372. * A 'struct static_key' uses a union such that it either points directly
  373. * to a table of 'struct jump_entry' or to a linked list of modules which in
  374. * turn point to 'struct jump_entry' tables.
  375. *
  376. * The two lower bits of the pointer are used to keep track of which pointer
  377. * type is in use and to store the initial branch direction, we use an access
  378. * function which preserves these bits.
  379. */
  380. static void static_key_set_entries(struct static_key *key,
  381. struct jump_entry *entries)
  382. {
  383. unsigned long type;
  384. WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
  385. type = key->type & JUMP_TYPE_MASK;
  386. key->entries = entries;
  387. key->type |= type;
  388. }
  389. static enum jump_label_type jump_label_type(struct jump_entry *entry)
  390. {
  391. struct static_key *key = jump_entry_key(entry);
  392. bool enabled = static_key_enabled(key);
  393. bool branch = jump_entry_is_branch(entry);
  394. /* See the comment in linux/jump_label.h */
  395. return enabled ^ branch;
  396. }
  397. static bool jump_label_can_update(struct jump_entry *entry, bool init)
  398. {
  399. /*
  400. * Cannot update code that was in an init text area.
  401. */
  402. if (!init && jump_entry_is_init(entry))
  403. return false;
  404. if (!kernel_text_address(jump_entry_code(entry))) {
  405. /*
  406. * This skips patching built-in __exit, which
  407. * is part of init_section_contains() but is
  408. * not part of kernel_text_address().
  409. *
  410. * Skipping built-in __exit is fine since it
  411. * will never be executed.
  412. */
  413. WARN_ONCE(!jump_entry_is_init(entry),
  414. "can't patch jump_label at %pS",
  415. (void *)jump_entry_code(entry));
  416. return false;
  417. }
  418. return true;
  419. }
  420. #ifndef HAVE_JUMP_LABEL_BATCH
  421. static void __jump_label_update(struct static_key *key,
  422. struct jump_entry *entry,
  423. struct jump_entry *stop,
  424. bool init)
  425. {
  426. for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
  427. if (jump_label_can_update(entry, init))
  428. arch_jump_label_transform(entry, jump_label_type(entry));
  429. }
  430. }
  431. #else
  432. static void __jump_label_update(struct static_key *key,
  433. struct jump_entry *entry,
  434. struct jump_entry *stop,
  435. bool init)
  436. {
  437. for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
  438. if (!jump_label_can_update(entry, init))
  439. continue;
  440. if (!arch_jump_label_transform_queue(entry, jump_label_type(entry))) {
  441. /*
  442. * Queue is full: Apply the current queue and try again.
  443. */
  444. arch_jump_label_transform_apply();
  445. BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry)));
  446. }
  447. }
  448. arch_jump_label_transform_apply();
  449. }
  450. #endif
  451. void __init jump_label_init(void)
  452. {
  453. struct jump_entry *iter_start = __start___jump_table;
  454. struct jump_entry *iter_stop = __stop___jump_table;
  455. struct static_key *key = NULL;
  456. struct jump_entry *iter;
  457. /*
  458. * Since we are initializing the static_key.enabled field with
  459. * with the 'raw' int values (to avoid pulling in atomic.h) in
  460. * jump_label.h, let's make sure that is safe. There are only two
  461. * cases to check since we initialize to 0 or 1.
  462. */
  463. BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
  464. BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
  465. if (static_key_initialized)
  466. return;
  467. cpus_read_lock();
  468. jump_label_lock();
  469. jump_label_sort_entries(iter_start, iter_stop);
  470. for (iter = iter_start; iter < iter_stop; iter++) {
  471. struct static_key *iterk;
  472. bool in_init;
  473. /* rewrite NOPs */
  474. if (jump_label_type(iter) == JUMP_LABEL_NOP)
  475. arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
  476. in_init = init_section_contains((void *)jump_entry_code(iter), 1);
  477. jump_entry_set_init(iter, in_init);
  478. iterk = jump_entry_key(iter);
  479. if (iterk == key)
  480. continue;
  481. key = iterk;
  482. static_key_set_entries(key, iter);
  483. }
  484. static_key_initialized = true;
  485. jump_label_unlock();
  486. cpus_read_unlock();
  487. }
  488. static inline bool static_key_sealed(struct static_key *key)
  489. {
  490. return (key->type & JUMP_TYPE_LINKED) && !(key->type & ~JUMP_TYPE_MASK);
  491. }
  492. static inline void static_key_seal(struct static_key *key)
  493. {
  494. unsigned long type = key->type & JUMP_TYPE_TRUE;
  495. key->type = JUMP_TYPE_LINKED | type;
  496. }
  497. void jump_label_init_ro(void)
  498. {
  499. struct jump_entry *iter_start = __start___jump_table;
  500. struct jump_entry *iter_stop = __stop___jump_table;
  501. struct jump_entry *iter;
  502. if (WARN_ON_ONCE(!static_key_initialized))
  503. return;
  504. cpus_read_lock();
  505. jump_label_lock();
  506. for (iter = iter_start; iter < iter_stop; iter++) {
  507. struct static_key *iterk = jump_entry_key(iter);
  508. if (!is_kernel_ro_after_init((unsigned long)iterk))
  509. continue;
  510. if (static_key_sealed(iterk))
  511. continue;
  512. static_key_seal(iterk);
  513. }
  514. jump_label_unlock();
  515. cpus_read_unlock();
  516. }
  517. #ifdef CONFIG_MODULES
  518. enum jump_label_type jump_label_init_type(struct jump_entry *entry)
  519. {
  520. struct static_key *key = jump_entry_key(entry);
  521. bool type = static_key_type(key);
  522. bool branch = jump_entry_is_branch(entry);
  523. /* See the comment in linux/jump_label.h */
  524. return type ^ branch;
  525. }
  526. struct static_key_mod {
  527. struct static_key_mod *next;
  528. struct jump_entry *entries;
  529. struct module *mod;
  530. };
  531. static inline struct static_key_mod *static_key_mod(struct static_key *key)
  532. {
  533. WARN_ON_ONCE(!static_key_linked(key));
  534. return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
  535. }
  536. /***
  537. * key->type and key->next are the same via union.
  538. * This sets key->next and preserves the type bits.
  539. *
  540. * See additional comments above static_key_set_entries().
  541. */
  542. static void static_key_set_mod(struct static_key *key,
  543. struct static_key_mod *mod)
  544. {
  545. unsigned long type;
  546. WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
  547. type = key->type & JUMP_TYPE_MASK;
  548. key->next = mod;
  549. key->type |= type;
  550. }
  551. static int __jump_label_mod_text_reserved(void *start, void *end)
  552. {
  553. struct module *mod;
  554. int ret;
  555. preempt_disable();
  556. mod = __module_text_address((unsigned long)start);
  557. WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
  558. if (!try_module_get(mod))
  559. mod = NULL;
  560. preempt_enable();
  561. if (!mod)
  562. return 0;
  563. ret = __jump_label_text_reserved(mod->jump_entries,
  564. mod->jump_entries + mod->num_jump_entries,
  565. start, end, mod->state == MODULE_STATE_COMING);
  566. module_put(mod);
  567. return ret;
  568. }
  569. static void __jump_label_mod_update(struct static_key *key)
  570. {
  571. struct static_key_mod *mod;
  572. for (mod = static_key_mod(key); mod; mod = mod->next) {
  573. struct jump_entry *stop;
  574. struct module *m;
  575. /*
  576. * NULL if the static_key is defined in a module
  577. * that does not use it
  578. */
  579. if (!mod->entries)
  580. continue;
  581. m = mod->mod;
  582. if (!m)
  583. stop = __stop___jump_table;
  584. else
  585. stop = m->jump_entries + m->num_jump_entries;
  586. __jump_label_update(key, mod->entries, stop,
  587. m && m->state == MODULE_STATE_COMING);
  588. }
  589. }
  590. static int jump_label_add_module(struct module *mod)
  591. {
  592. struct jump_entry *iter_start = mod->jump_entries;
  593. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  594. struct jump_entry *iter;
  595. struct static_key *key = NULL;
  596. struct static_key_mod *jlm, *jlm2;
  597. /* if the module doesn't have jump label entries, just return */
  598. if (iter_start == iter_stop)
  599. return 0;
  600. jump_label_sort_entries(iter_start, iter_stop);
  601. for (iter = iter_start; iter < iter_stop; iter++) {
  602. struct static_key *iterk;
  603. bool in_init;
  604. in_init = within_module_init(jump_entry_code(iter), mod);
  605. jump_entry_set_init(iter, in_init);
  606. iterk = jump_entry_key(iter);
  607. if (iterk == key)
  608. continue;
  609. key = iterk;
  610. if (within_module((unsigned long)key, mod)) {
  611. static_key_set_entries(key, iter);
  612. continue;
  613. }
  614. /*
  615. * If the key was sealed at init, then there's no need to keep a
  616. * reference to its module entries - just patch them now and be
  617. * done with it.
  618. */
  619. if (static_key_sealed(key))
  620. goto do_poke;
  621. jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
  622. if (!jlm)
  623. return -ENOMEM;
  624. if (!static_key_linked(key)) {
  625. jlm2 = kzalloc(sizeof(struct static_key_mod),
  626. GFP_KERNEL);
  627. if (!jlm2) {
  628. kfree(jlm);
  629. return -ENOMEM;
  630. }
  631. preempt_disable();
  632. jlm2->mod = __module_address((unsigned long)key);
  633. preempt_enable();
  634. jlm2->entries = static_key_entries(key);
  635. jlm2->next = NULL;
  636. static_key_set_mod(key, jlm2);
  637. static_key_set_linked(key);
  638. }
  639. jlm->mod = mod;
  640. jlm->entries = iter;
  641. jlm->next = static_key_mod(key);
  642. static_key_set_mod(key, jlm);
  643. static_key_set_linked(key);
  644. /* Only update if we've changed from our initial state */
  645. do_poke:
  646. if (jump_label_type(iter) != jump_label_init_type(iter))
  647. __jump_label_update(key, iter, iter_stop, true);
  648. }
  649. return 0;
  650. }
  651. static void jump_label_del_module(struct module *mod)
  652. {
  653. struct jump_entry *iter_start = mod->jump_entries;
  654. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  655. struct jump_entry *iter;
  656. struct static_key *key = NULL;
  657. struct static_key_mod *jlm, **prev;
  658. for (iter = iter_start; iter < iter_stop; iter++) {
  659. if (jump_entry_key(iter) == key)
  660. continue;
  661. key = jump_entry_key(iter);
  662. if (within_module((unsigned long)key, mod))
  663. continue;
  664. /* No @jlm allocated because key was sealed at init. */
  665. if (static_key_sealed(key))
  666. continue;
  667. /* No memory during module load */
  668. if (WARN_ON(!static_key_linked(key)))
  669. continue;
  670. prev = &key->next;
  671. jlm = static_key_mod(key);
  672. while (jlm && jlm->mod != mod) {
  673. prev = &jlm->next;
  674. jlm = jlm->next;
  675. }
  676. /* No memory during module load */
  677. if (WARN_ON(!jlm))
  678. continue;
  679. if (prev == &key->next)
  680. static_key_set_mod(key, jlm->next);
  681. else
  682. *prev = jlm->next;
  683. kfree(jlm);
  684. jlm = static_key_mod(key);
  685. /* if only one etry is left, fold it back into the static_key */
  686. if (jlm->next == NULL) {
  687. static_key_set_entries(key, jlm->entries);
  688. static_key_clear_linked(key);
  689. kfree(jlm);
  690. }
  691. }
  692. }
  693. static int
  694. jump_label_module_notify(struct notifier_block *self, unsigned long val,
  695. void *data)
  696. {
  697. struct module *mod = data;
  698. int ret = 0;
  699. cpus_read_lock();
  700. jump_label_lock();
  701. switch (val) {
  702. case MODULE_STATE_COMING:
  703. ret = jump_label_add_module(mod);
  704. if (ret) {
  705. WARN(1, "Failed to allocate memory: jump_label may not work properly.\n");
  706. jump_label_del_module(mod);
  707. }
  708. break;
  709. case MODULE_STATE_GOING:
  710. jump_label_del_module(mod);
  711. break;
  712. }
  713. jump_label_unlock();
  714. cpus_read_unlock();
  715. return notifier_from_errno(ret);
  716. }
  717. static struct notifier_block jump_label_module_nb = {
  718. .notifier_call = jump_label_module_notify,
  719. .priority = 1, /* higher than tracepoints */
  720. };
  721. static __init int jump_label_init_module(void)
  722. {
  723. return register_module_notifier(&jump_label_module_nb);
  724. }
  725. early_initcall(jump_label_init_module);
  726. #endif /* CONFIG_MODULES */
  727. /***
  728. * jump_label_text_reserved - check if addr range is reserved
  729. * @start: start text addr
  730. * @end: end text addr
  731. *
  732. * checks if the text addr located between @start and @end
  733. * overlaps with any of the jump label patch addresses. Code
  734. * that wants to modify kernel text should first verify that
  735. * it does not overlap with any of the jump label addresses.
  736. * Caller must hold jump_label_mutex.
  737. *
  738. * returns 1 if there is an overlap, 0 otherwise
  739. */
  740. int jump_label_text_reserved(void *start, void *end)
  741. {
  742. bool init = system_state < SYSTEM_RUNNING;
  743. int ret = __jump_label_text_reserved(__start___jump_table,
  744. __stop___jump_table, start, end, init);
  745. if (ret)
  746. return ret;
  747. #ifdef CONFIG_MODULES
  748. ret = __jump_label_mod_text_reserved(start, end);
  749. #endif
  750. return ret;
  751. }
  752. static void jump_label_update(struct static_key *key)
  753. {
  754. struct jump_entry *stop = __stop___jump_table;
  755. bool init = system_state < SYSTEM_RUNNING;
  756. struct jump_entry *entry;
  757. #ifdef CONFIG_MODULES
  758. struct module *mod;
  759. if (static_key_linked(key)) {
  760. __jump_label_mod_update(key);
  761. return;
  762. }
  763. preempt_disable();
  764. mod = __module_address((unsigned long)key);
  765. if (mod) {
  766. stop = mod->jump_entries + mod->num_jump_entries;
  767. init = mod->state == MODULE_STATE_COMING;
  768. }
  769. preempt_enable();
  770. #endif
  771. entry = static_key_entries(key);
  772. /* if there are no users, entry can be NULL */
  773. if (entry)
  774. __jump_label_update(key, entry, stop, init);
  775. }
  776. #ifdef CONFIG_STATIC_KEYS_SELFTEST
  777. static DEFINE_STATIC_KEY_TRUE(sk_true);
  778. static DEFINE_STATIC_KEY_FALSE(sk_false);
  779. static __init int jump_label_test(void)
  780. {
  781. int i;
  782. for (i = 0; i < 2; i++) {
  783. WARN_ON(static_key_enabled(&sk_true.key) != true);
  784. WARN_ON(static_key_enabled(&sk_false.key) != false);
  785. WARN_ON(!static_branch_likely(&sk_true));
  786. WARN_ON(!static_branch_unlikely(&sk_true));
  787. WARN_ON(static_branch_likely(&sk_false));
  788. WARN_ON(static_branch_unlikely(&sk_false));
  789. static_branch_disable(&sk_true);
  790. static_branch_enable(&sk_false);
  791. WARN_ON(static_key_enabled(&sk_true.key) == true);
  792. WARN_ON(static_key_enabled(&sk_false.key) == false);
  793. WARN_ON(static_branch_likely(&sk_true));
  794. WARN_ON(static_branch_unlikely(&sk_true));
  795. WARN_ON(!static_branch_likely(&sk_false));
  796. WARN_ON(!static_branch_unlikely(&sk_false));
  797. static_branch_enable(&sk_true);
  798. static_branch_disable(&sk_false);
  799. }
  800. return 0;
  801. }
  802. early_initcall(jump_label_test);
  803. #endif /* STATIC_KEYS_SELFTEST */