Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 2 | /* |
| 3 | * jump label support |
| 4 | * |
| 5 | * Copyright (C) 2009 Jason Baron <jbaron@redhat.com> |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 6 | * Copyright (C) 2011 Peter Zijlstra |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 7 | * |
| 8 | */ |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 9 | #include <linux/memory.h> |
| 10 | #include <linux/uaccess.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/list.h> |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/sort.h> |
| 15 | #include <linux/err.h> |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 16 | #include <linux/static_key.h> |
Andrew Jones | 851cf6e | 2013-08-09 19:51:57 +0530 | [diff] [blame] | 17 | #include <linux/jump_label_ratelimit.h> |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 18 | #include <linux/bug.h> |
Thomas Gleixner | f2545b2d | 2017-05-24 10:15:35 +0200 | [diff] [blame] | 19 | #include <linux/cpu.h> |
Josh Poimboeuf | 578ae44 | 2018-03-19 13:18:57 -0500 | [diff] [blame] | 20 | #include <asm/sections.h> |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 21 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 22 | /* mutex to protect coming/going of the the jump_label table */ |
| 23 | static DEFINE_MUTEX(jump_label_mutex); |
| 24 | |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 25 | void jump_label_lock(void) |
| 26 | { |
| 27 | mutex_lock(&jump_label_mutex); |
| 28 | } |
| 29 | |
| 30 | void jump_label_unlock(void) |
| 31 | { |
| 32 | mutex_unlock(&jump_label_mutex); |
| 33 | } |
| 34 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 35 | static int jump_label_cmp(const void *a, const void *b) |
| 36 | { |
| 37 | const struct jump_entry *jea = a; |
| 38 | const struct jump_entry *jeb = b; |
| 39 | |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 40 | if (jump_entry_key(jea) < jump_entry_key(jeb)) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 41 | return -1; |
| 42 | |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 43 | if (jump_entry_key(jea) > jump_entry_key(jeb)) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 44 | return 1; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Ard Biesheuvel | 50ff18a | 2018-09-18 23:51:37 -0700 | [diff] [blame] | 49 | static void jump_label_swap(void *a, void *b, int size) |
| 50 | { |
| 51 | long delta = (unsigned long)a - (unsigned long)b; |
| 52 | struct jump_entry *jea = a; |
| 53 | struct jump_entry *jeb = b; |
| 54 | struct jump_entry tmp = *jea; |
| 55 | |
| 56 | jea->code = jeb->code - delta; |
| 57 | jea->target = jeb->target - delta; |
| 58 | jea->key = jeb->key - delta; |
| 59 | |
| 60 | jeb->code = tmp.code + delta; |
| 61 | jeb->target = tmp.target + delta; |
| 62 | jeb->key = tmp.key + delta; |
| 63 | } |
| 64 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 65 | static void |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 66 | jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 67 | { |
| 68 | unsigned long size; |
Ard Biesheuvel | 50ff18a | 2018-09-18 23:51:37 -0700 | [diff] [blame] | 69 | void *swapfn = NULL; |
| 70 | |
| 71 | if (IS_ENABLED(CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE)) |
| 72 | swapfn = jump_label_swap; |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 73 | |
| 74 | size = (((unsigned long)stop - (unsigned long)start) |
| 75 | / sizeof(struct jump_entry)); |
Ard Biesheuvel | 50ff18a | 2018-09-18 23:51:37 -0700 | [diff] [blame] | 76 | sort(start, size, sizeof(struct jump_entry), jump_label_cmp, swapfn); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 79 | static void jump_label_update(struct static_key *key); |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 80 | |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 81 | /* |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 82 | * There are similar definitions for the !CONFIG_JUMP_LABEL case in jump_label.h. |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 83 | * The use of 'atomic_read()' requires atomic.h and its problematic for some |
| 84 | * kernel headers such as kernel.h and others. Since static_key_count() is not |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 85 | * used in the branch statements as it is for the !CONFIG_JUMP_LABEL case its ok |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 86 | * to have it be a function here. Similarly, for 'static_key_enable()' and |
| 87 | * 'static_key_disable()', which require bug.h. This should allow jump_label.h |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 88 | * to be included from most/all places for CONFIG_JUMP_LABEL. |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 89 | */ |
| 90 | int static_key_count(struct static_key *key) |
| 91 | { |
| 92 | /* |
| 93 | * -1 means the first static_key_slow_inc() is in progress. |
| 94 | * static_key_enabled() must return true, so return 1 here. |
| 95 | */ |
| 96 | int n = atomic_read(&key->enabled); |
| 97 | |
| 98 | return n >= 0 ? n : 1; |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(static_key_count); |
| 101 | |
Peter Zijlstra | ce48c146 | 2018-01-22 22:53:28 +0100 | [diff] [blame] | 102 | void static_key_slow_inc_cpuslocked(struct static_key *key) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 103 | { |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 104 | int v, v1; |
| 105 | |
Borislav Petkov | 5cdda51 | 2017-10-18 17:24:28 +0200 | [diff] [blame] | 106 | STATIC_KEY_CHECK_USE(key); |
Peter Zijlstra | cb53826 | 2018-07-31 14:35:32 +0200 | [diff] [blame] | 107 | lockdep_assert_cpus_held(); |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Careful if we get concurrent static_key_slow_inc() calls; |
| 111 | * later calls must wait for the first one to _finish_ the |
| 112 | * jump_label_update() process. At the same time, however, |
| 113 | * the jump_label_update() call below wants to see |
| 114 | * static_key_enabled(&key) for jumps to be updated properly. |
| 115 | * |
| 116 | * So give a special meaning to negative key->enabled: it sends |
| 117 | * static_key_slow_inc() down the slow path, and it is non-zero |
| 118 | * so it counts as "enabled" in jump_label_update(). Note that |
| 119 | * atomic_inc_unless_negative() checks >= 0, so roll our own. |
| 120 | */ |
| 121 | for (v = atomic_read(&key->enabled); v > 0; v = v1) { |
| 122 | v1 = atomic_cmpxchg(&key->enabled, v, v + 1); |
Marc Zyngier | 8b7b412 | 2017-08-01 09:02:55 +0100 | [diff] [blame] | 123 | if (likely(v1 == v)) |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 124 | return; |
| 125 | } |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 126 | |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 127 | jump_label_lock(); |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 128 | if (atomic_read(&key->enabled) == 0) { |
| 129 | atomic_set(&key->enabled, -1); |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 130 | jump_label_update(key); |
Peter Zijlstra | d0646a6 | 2017-08-01 23:58:50 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Ensure that if the above cmpxchg loop observes our positive |
| 133 | * value, it must also observe all the text changes. |
| 134 | */ |
| 135 | atomic_set_release(&key->enabled, 1); |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 136 | } else { |
| 137 | atomic_inc(&key->enabled); |
| 138 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 139 | jump_label_unlock(); |
Marc Zyngier | 8b7b412 | 2017-08-01 09:02:55 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void static_key_slow_inc(struct static_key *key) |
| 143 | { |
| 144 | cpus_read_lock(); |
| 145 | static_key_slow_inc_cpuslocked(key); |
Thomas Gleixner | f2545b2d | 2017-05-24 10:15:35 +0200 | [diff] [blame] | 146 | cpus_read_unlock(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 147 | } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 148 | EXPORT_SYMBOL_GPL(static_key_slow_inc); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 149 | |
Marc Zyngier | 5a40527 | 2017-08-01 09:02:56 +0100 | [diff] [blame] | 150 | void static_key_enable_cpuslocked(struct static_key *key) |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 151 | { |
Borislav Petkov | 5cdda51 | 2017-10-18 17:24:28 +0200 | [diff] [blame] | 152 | STATIC_KEY_CHECK_USE(key); |
Peter Zijlstra | cb53826 | 2018-07-31 14:35:32 +0200 | [diff] [blame] | 153 | lockdep_assert_cpus_held(); |
Marc Zyngier | 5a40527 | 2017-08-01 09:02:56 +0100 | [diff] [blame] | 154 | |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 155 | if (atomic_read(&key->enabled) > 0) { |
| 156 | WARN_ON_ONCE(atomic_read(&key->enabled) != 1); |
| 157 | return; |
| 158 | } |
| 159 | |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 160 | jump_label_lock(); |
| 161 | if (atomic_read(&key->enabled) == 0) { |
| 162 | atomic_set(&key->enabled, -1); |
| 163 | jump_label_update(key); |
Peter Zijlstra | d0646a6 | 2017-08-01 23:58:50 +0200 | [diff] [blame] | 164 | /* |
| 165 | * See static_key_slow_inc(). |
| 166 | */ |
| 167 | atomic_set_release(&key->enabled, 1); |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 168 | } |
| 169 | jump_label_unlock(); |
Marc Zyngier | 5a40527 | 2017-08-01 09:02:56 +0100 | [diff] [blame] | 170 | } |
| 171 | EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked); |
| 172 | |
| 173 | void static_key_enable(struct static_key *key) |
| 174 | { |
| 175 | cpus_read_lock(); |
| 176 | static_key_enable_cpuslocked(key); |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 177 | cpus_read_unlock(); |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(static_key_enable); |
| 180 | |
Marc Zyngier | 5a40527 | 2017-08-01 09:02:56 +0100 | [diff] [blame] | 181 | void static_key_disable_cpuslocked(struct static_key *key) |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 182 | { |
Borislav Petkov | 5cdda51 | 2017-10-18 17:24:28 +0200 | [diff] [blame] | 183 | STATIC_KEY_CHECK_USE(key); |
Peter Zijlstra | cb53826 | 2018-07-31 14:35:32 +0200 | [diff] [blame] | 184 | lockdep_assert_cpus_held(); |
Marc Zyngier | 5a40527 | 2017-08-01 09:02:56 +0100 | [diff] [blame] | 185 | |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 186 | if (atomic_read(&key->enabled) != 1) { |
| 187 | WARN_ON_ONCE(atomic_read(&key->enabled) != 0); |
| 188 | return; |
| 189 | } |
| 190 | |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 191 | jump_label_lock(); |
| 192 | if (atomic_cmpxchg(&key->enabled, 1, 0)) |
| 193 | jump_label_update(key); |
| 194 | jump_label_unlock(); |
Marc Zyngier | 5a40527 | 2017-08-01 09:02:56 +0100 | [diff] [blame] | 195 | } |
| 196 | EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked); |
| 197 | |
| 198 | void static_key_disable(struct static_key *key) |
| 199 | { |
| 200 | cpus_read_lock(); |
| 201 | static_key_disable_cpuslocked(key); |
Paolo Bonzini | 1dbb670 | 2017-08-01 17:24:04 +0200 | [diff] [blame] | 202 | cpus_read_unlock(); |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(static_key_disable); |
| 205 | |
Jakub Kicinski | b92e793 | 2019-03-29 17:08:53 -0700 | [diff] [blame] | 206 | static bool static_key_slow_try_dec(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 207 | { |
Peter Zijlstra | a1247d0 | 2019-03-19 13:18:56 +0100 | [diff] [blame] | 208 | int val; |
| 209 | |
Jakub Kicinski | b92e793 | 2019-03-29 17:08:53 -0700 | [diff] [blame] | 210 | val = atomic_fetch_add_unless(&key->enabled, -1, 1); |
| 211 | if (val == 1) |
| 212 | return false; |
Peter Zijlstra | cb53826 | 2018-07-31 14:35:32 +0200 | [diff] [blame] | 213 | |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 214 | /* |
| 215 | * The negative count check is valid even when a negative |
| 216 | * key->enabled is in use by static_key_slow_inc(); a |
| 217 | * __static_key_slow_dec() before the first static_key_slow_inc() |
| 218 | * returns is unbalanced, because all other static_key_slow_inc() |
| 219 | * instances block while the update is in progress. |
| 220 | */ |
Jakub Kicinski | b92e793 | 2019-03-29 17:08:53 -0700 | [diff] [blame] | 221 | WARN(val < 0, "jump label: negative count!\n"); |
| 222 | return true; |
| 223 | } |
| 224 | |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 225 | static void __static_key_slow_dec_cpuslocked(struct static_key *key) |
Jakub Kicinski | b92e793 | 2019-03-29 17:08:53 -0700 | [diff] [blame] | 226 | { |
| 227 | lockdep_assert_cpus_held(); |
| 228 | |
| 229 | if (static_key_slow_try_dec(key)) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 230 | return; |
| 231 | |
Peter Zijlstra | a1247d0 | 2019-03-19 13:18:56 +0100 | [diff] [blame] | 232 | jump_label_lock(); |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 233 | if (atomic_dec_and_test(&key->enabled)) |
| 234 | jump_label_update(key); |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 235 | jump_label_unlock(); |
Marc Zyngier | 8b7b412 | 2017-08-01 09:02:55 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 238 | static void __static_key_slow_dec(struct static_key *key) |
Marc Zyngier | 8b7b412 | 2017-08-01 09:02:55 +0100 | [diff] [blame] | 239 | { |
| 240 | cpus_read_lock(); |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 241 | __static_key_slow_dec_cpuslocked(key); |
Thomas Gleixner | f2545b2d | 2017-05-24 10:15:35 +0200 | [diff] [blame] | 242 | cpus_read_unlock(); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Jakub Kicinski | ad282a8 | 2019-03-29 17:08:52 -0700 | [diff] [blame] | 245 | void jump_label_update_timeout(struct work_struct *work) |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 246 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 247 | struct static_key_deferred *key = |
| 248 | container_of(work, struct static_key_deferred, work.work); |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 249 | __static_key_slow_dec(&key->key); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 250 | } |
Jakub Kicinski | ad282a8 | 2019-03-29 17:08:52 -0700 | [diff] [blame] | 251 | EXPORT_SYMBOL_GPL(jump_label_update_timeout); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 252 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 253 | void static_key_slow_dec(struct static_key *key) |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 254 | { |
Borislav Petkov | 5cdda51 | 2017-10-18 17:24:28 +0200 | [diff] [blame] | 255 | STATIC_KEY_CHECK_USE(key); |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 256 | __static_key_slow_dec(key); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 257 | } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 258 | EXPORT_SYMBOL_GPL(static_key_slow_dec); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 259 | |
Peter Zijlstra | ce48c146 | 2018-01-22 22:53:28 +0100 | [diff] [blame] | 260 | void static_key_slow_dec_cpuslocked(struct static_key *key) |
| 261 | { |
| 262 | STATIC_KEY_CHECK_USE(key); |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 263 | __static_key_slow_dec_cpuslocked(key); |
Peter Zijlstra | ce48c146 | 2018-01-22 22:53:28 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Jakub Kicinski | ad282a8 | 2019-03-29 17:08:52 -0700 | [diff] [blame] | 266 | void __static_key_slow_dec_deferred(struct static_key *key, |
| 267 | struct delayed_work *work, |
| 268 | unsigned long timeout) |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 269 | { |
Borislav Petkov | 5cdda51 | 2017-10-18 17:24:28 +0200 | [diff] [blame] | 270 | STATIC_KEY_CHECK_USE(key); |
Jakub Kicinski | 94b5f31 | 2019-03-29 17:08:54 -0700 | [diff] [blame] | 271 | |
| 272 | if (static_key_slow_try_dec(key)) |
| 273 | return; |
| 274 | |
| 275 | schedule_delayed_work(work, timeout); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 276 | } |
Jakub Kicinski | ad282a8 | 2019-03-29 17:08:52 -0700 | [diff] [blame] | 277 | EXPORT_SYMBOL_GPL(__static_key_slow_dec_deferred); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 278 | |
Jakub Kicinski | ad282a8 | 2019-03-29 17:08:52 -0700 | [diff] [blame] | 279 | void __static_key_deferred_flush(void *key, struct delayed_work *work) |
David Matlack | b6416e6 | 2016-12-16 14:30:35 -0800 | [diff] [blame] | 280 | { |
Borislav Petkov | 5cdda51 | 2017-10-18 17:24:28 +0200 | [diff] [blame] | 281 | STATIC_KEY_CHECK_USE(key); |
Jakub Kicinski | ad282a8 | 2019-03-29 17:08:52 -0700 | [diff] [blame] | 282 | flush_delayed_work(work); |
David Matlack | b6416e6 | 2016-12-16 14:30:35 -0800 | [diff] [blame] | 283 | } |
Jakub Kicinski | ad282a8 | 2019-03-29 17:08:52 -0700 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(__static_key_deferred_flush); |
David Matlack | b6416e6 | 2016-12-16 14:30:35 -0800 | [diff] [blame] | 285 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 286 | void jump_label_rate_limit(struct static_key_deferred *key, |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 287 | unsigned long rl) |
| 288 | { |
Borislav Petkov | 5cdda51 | 2017-10-18 17:24:28 +0200 | [diff] [blame] | 289 | STATIC_KEY_CHECK_USE(key); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 290 | key->timeout = rl; |
| 291 | INIT_DELAYED_WORK(&key->work, jump_label_update_timeout); |
| 292 | } |
Gleb Natapov | a181dc1 | 2012-08-05 15:58:29 +0300 | [diff] [blame] | 293 | EXPORT_SYMBOL_GPL(jump_label_rate_limit); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 294 | |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 295 | static int addr_conflict(struct jump_entry *entry, void *start, void *end) |
| 296 | { |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 297 | if (jump_entry_code(entry) <= (unsigned long)end && |
| 298 | jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE > (unsigned long)start) |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 299 | return 1; |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 304 | static int __jump_label_text_reserved(struct jump_entry *iter_start, |
| 305 | struct jump_entry *iter_stop, void *start, void *end) |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 306 | { |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 307 | struct jump_entry *iter; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 308 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 309 | iter = iter_start; |
| 310 | while (iter < iter_stop) { |
| 311 | if (addr_conflict(iter, start, end)) |
| 312 | return 1; |
| 313 | iter++; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 314 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 315 | |
| 316 | return 0; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 319 | /* |
Jeremy Fitzhardinge | 20284aa | 2011-10-03 11:01:46 -0700 | [diff] [blame] | 320 | * Update code which is definitely not currently executing. |
| 321 | * Architectures which need heavyweight synchronization to modify |
| 322 | * running code can override this to make the non-live update case |
| 323 | * cheaper. |
| 324 | */ |
Peter Zijlstra | 9cdbe1c | 2011-12-06 17:27:29 +0100 | [diff] [blame] | 325 | void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry, |
Jeremy Fitzhardinge | 20284aa | 2011-10-03 11:01:46 -0700 | [diff] [blame] | 326 | enum jump_label_type type) |
| 327 | { |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 328 | arch_jump_label_transform(entry, type); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 329 | } |
| 330 | |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 331 | static inline struct jump_entry *static_key_entries(struct static_key *key) |
| 332 | { |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 333 | WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED); |
| 334 | return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK); |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 337 | static inline bool static_key_type(struct static_key *key) |
| 338 | { |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 339 | return key->type & JUMP_TYPE_TRUE; |
| 340 | } |
| 341 | |
| 342 | static inline bool static_key_linked(struct static_key *key) |
| 343 | { |
| 344 | return key->type & JUMP_TYPE_LINKED; |
| 345 | } |
| 346 | |
| 347 | static inline void static_key_clear_linked(struct static_key *key) |
| 348 | { |
| 349 | key->type &= ~JUMP_TYPE_LINKED; |
| 350 | } |
| 351 | |
| 352 | static inline void static_key_set_linked(struct static_key *key) |
| 353 | { |
| 354 | key->type |= JUMP_TYPE_LINKED; |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 357 | /*** |
| 358 | * A 'struct static_key' uses a union such that it either points directly |
| 359 | * to a table of 'struct jump_entry' or to a linked list of modules which in |
| 360 | * turn point to 'struct jump_entry' tables. |
| 361 | * |
| 362 | * The two lower bits of the pointer are used to keep track of which pointer |
| 363 | * type is in use and to store the initial branch direction, we use an access |
| 364 | * function which preserves these bits. |
| 365 | */ |
| 366 | static void static_key_set_entries(struct static_key *key, |
| 367 | struct jump_entry *entries) |
| 368 | { |
| 369 | unsigned long type; |
| 370 | |
| 371 | WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK); |
| 372 | type = key->type & JUMP_TYPE_MASK; |
| 373 | key->entries = entries; |
| 374 | key->type |= type; |
| 375 | } |
| 376 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 377 | static enum jump_label_type jump_label_type(struct jump_entry *entry) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 378 | { |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 379 | struct static_key *key = jump_entry_key(entry); |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 380 | bool enabled = static_key_enabled(key); |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 381 | bool branch = jump_entry_is_branch(entry); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 382 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 383 | /* See the comment in linux/jump_label.h */ |
| 384 | return enabled ^ branch; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 387 | static void __jump_label_update(struct static_key *key, |
| 388 | struct jump_entry *entry, |
Ard Biesheuvel | 1948367 | 2018-09-18 23:51:42 -0700 | [diff] [blame] | 389 | struct jump_entry *stop, |
| 390 | bool init) |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 391 | { |
| 392 | for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) { |
| 393 | /* |
Josh Poimboeuf | dc1dd18 | 2018-02-20 11:37:52 -0600 | [diff] [blame] | 394 | * An entry->code of 0 indicates an entry which has been |
| 395 | * disabled because it was in an init text area. |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 396 | */ |
Ard Biesheuvel | 1948367 | 2018-09-18 23:51:42 -0700 | [diff] [blame] | 397 | if (init || !jump_entry_is_init(entry)) { |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 398 | if (kernel_text_address(jump_entry_code(entry))) |
Josh Poimboeuf | dc1dd18 | 2018-02-20 11:37:52 -0600 | [diff] [blame] | 399 | arch_jump_label_transform(entry, jump_label_type(entry)); |
| 400 | else |
Josh Poimboeuf | af1d830 | 2018-03-14 10:24:20 -0500 | [diff] [blame] | 401 | WARN_ONCE(1, "can't patch jump_label at %pS", |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 402 | (void *)jump_entry_code(entry)); |
Josh Poimboeuf | dc1dd18 | 2018-02-20 11:37:52 -0600 | [diff] [blame] | 403 | } |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 407 | void __init jump_label_init(void) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 408 | { |
| 409 | struct jump_entry *iter_start = __start___jump_table; |
| 410 | struct jump_entry *iter_stop = __stop___jump_table; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 411 | struct static_key *key = NULL; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 412 | struct jump_entry *iter; |
| 413 | |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 414 | /* |
| 415 | * Since we are initializing the static_key.enabled field with |
| 416 | * with the 'raw' int values (to avoid pulling in atomic.h) in |
| 417 | * jump_label.h, let's make sure that is safe. There are only two |
| 418 | * cases to check since we initialize to 0 or 1. |
| 419 | */ |
| 420 | BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0); |
| 421 | BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1); |
| 422 | |
Kevin Hao | e3f9108 | 2016-07-23 14:42:37 +0530 | [diff] [blame] | 423 | if (static_key_initialized) |
| 424 | return; |
| 425 | |
Thomas Gleixner | f2545b2d | 2017-05-24 10:15:35 +0200 | [diff] [blame] | 426 | cpus_read_lock(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 427 | jump_label_lock(); |
| 428 | jump_label_sort_entries(iter_start, iter_stop); |
| 429 | |
| 430 | for (iter = iter_start; iter < iter_stop; iter++) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 431 | struct static_key *iterk; |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 432 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 433 | /* rewrite NOPs */ |
| 434 | if (jump_label_type(iter) == JUMP_LABEL_NOP) |
| 435 | arch_jump_label_transform_static(iter, JUMP_LABEL_NOP); |
| 436 | |
Ard Biesheuvel | 1948367 | 2018-09-18 23:51:42 -0700 | [diff] [blame] | 437 | if (init_section_contains((void *)jump_entry_code(iter), 1)) |
| 438 | jump_entry_set_init(iter); |
| 439 | |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 440 | iterk = jump_entry_key(iter); |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 441 | if (iterk == key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 442 | continue; |
| 443 | |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 444 | key = iterk; |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 445 | static_key_set_entries(key, iter); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 446 | } |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 447 | static_key_initialized = true; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 448 | jump_label_unlock(); |
Thomas Gleixner | f2545b2d | 2017-05-24 10:15:35 +0200 | [diff] [blame] | 449 | cpus_read_unlock(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 450 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 451 | |
| 452 | #ifdef CONFIG_MODULES |
| 453 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 454 | static enum jump_label_type jump_label_init_type(struct jump_entry *entry) |
| 455 | { |
| 456 | struct static_key *key = jump_entry_key(entry); |
| 457 | bool type = static_key_type(key); |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 458 | bool branch = jump_entry_is_branch(entry); |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 459 | |
| 460 | /* See the comment in linux/jump_label.h */ |
| 461 | return type ^ branch; |
| 462 | } |
| 463 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 464 | struct static_key_mod { |
| 465 | struct static_key_mod *next; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 466 | struct jump_entry *entries; |
| 467 | struct module *mod; |
| 468 | }; |
| 469 | |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 470 | static inline struct static_key_mod *static_key_mod(struct static_key *key) |
| 471 | { |
Borislav Petkov | 34e12b8 | 2018-09-09 13:42:52 +0200 | [diff] [blame] | 472 | WARN_ON_ONCE(!static_key_linked(key)); |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 473 | return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK); |
| 474 | } |
| 475 | |
| 476 | /*** |
| 477 | * key->type and key->next are the same via union. |
| 478 | * This sets key->next and preserves the type bits. |
| 479 | * |
| 480 | * See additional comments above static_key_set_entries(). |
| 481 | */ |
| 482 | static void static_key_set_mod(struct static_key *key, |
| 483 | struct static_key_mod *mod) |
| 484 | { |
| 485 | unsigned long type; |
| 486 | |
| 487 | WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK); |
| 488 | type = key->type & JUMP_TYPE_MASK; |
| 489 | key->next = mod; |
| 490 | key->type |= type; |
| 491 | } |
| 492 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 493 | static int __jump_label_mod_text_reserved(void *start, void *end) |
| 494 | { |
| 495 | struct module *mod; |
| 496 | |
Rusty Russell | bdc9f37 | 2016-07-27 12:17:35 +0930 | [diff] [blame] | 497 | preempt_disable(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 498 | mod = __module_text_address((unsigned long)start); |
Rusty Russell | bdc9f37 | 2016-07-27 12:17:35 +0930 | [diff] [blame] | 499 | WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod); |
| 500 | preempt_enable(); |
| 501 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 502 | if (!mod) |
| 503 | return 0; |
| 504 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 505 | |
| 506 | return __jump_label_text_reserved(mod->jump_entries, |
| 507 | mod->jump_entries + mod->num_jump_entries, |
| 508 | start, end); |
| 509 | } |
| 510 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 511 | static void __jump_label_mod_update(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 512 | { |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 513 | struct static_key_mod *mod; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 514 | |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 515 | for (mod = static_key_mod(key); mod; mod = mod->next) { |
| 516 | struct jump_entry *stop; |
| 517 | struct module *m; |
Jiri Olsa | 7cbc5b8 | 2011-05-10 12:43:46 +0200 | [diff] [blame] | 518 | |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 519 | /* |
| 520 | * NULL if the static_key is defined in a module |
| 521 | * that does not use it |
| 522 | */ |
| 523 | if (!mod->entries) |
| 524 | continue; |
| 525 | |
| 526 | m = mod->mod; |
| 527 | if (!m) |
| 528 | stop = __stop___jump_table; |
| 529 | else |
| 530 | stop = m->jump_entries + m->num_jump_entries; |
Ard Biesheuvel | 1948367 | 2018-09-18 23:51:42 -0700 | [diff] [blame] | 531 | __jump_label_update(key, mod->entries, stop, |
Ard Biesheuvel | 77ac1c0 | 2018-10-01 10:13:24 +0200 | [diff] [blame] | 532 | m && m->state == MODULE_STATE_COMING); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | /*** |
| 537 | * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop() |
| 538 | * @mod: module to patch |
| 539 | * |
| 540 | * Allow for run-time selection of the optimal nops. Before the module |
| 541 | * loads patch these with arch_get_jump_label_nop(), which is specified by |
| 542 | * the arch specific jump label code. |
| 543 | */ |
| 544 | void jump_label_apply_nops(struct module *mod) |
| 545 | { |
| 546 | struct jump_entry *iter_start = mod->jump_entries; |
| 547 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
| 548 | struct jump_entry *iter; |
| 549 | |
| 550 | /* if the module doesn't have jump label entries, just return */ |
| 551 | if (iter_start == iter_stop) |
| 552 | return; |
| 553 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 554 | for (iter = iter_start; iter < iter_stop; iter++) { |
| 555 | /* Only write NOPs for arch_branch_static(). */ |
| 556 | if (jump_label_init_type(iter) == JUMP_LABEL_NOP) |
| 557 | arch_jump_label_transform_static(iter, JUMP_LABEL_NOP); |
| 558 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static int jump_label_add_module(struct module *mod) |
| 562 | { |
| 563 | struct jump_entry *iter_start = mod->jump_entries; |
| 564 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
| 565 | struct jump_entry *iter; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 566 | struct static_key *key = NULL; |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 567 | struct static_key_mod *jlm, *jlm2; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 568 | |
| 569 | /* if the module doesn't have jump label entries, just return */ |
| 570 | if (iter_start == iter_stop) |
| 571 | return 0; |
| 572 | |
| 573 | jump_label_sort_entries(iter_start, iter_stop); |
| 574 | |
| 575 | for (iter = iter_start; iter < iter_stop; iter++) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 576 | struct static_key *iterk; |
| 577 | |
Ard Biesheuvel | 1948367 | 2018-09-18 23:51:42 -0700 | [diff] [blame] | 578 | if (within_module_init(jump_entry_code(iter), mod)) |
| 579 | jump_entry_set_init(iter); |
| 580 | |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 581 | iterk = jump_entry_key(iter); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 582 | if (iterk == key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 583 | continue; |
| 584 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 585 | key = iterk; |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 586 | if (within_module((unsigned long)key, mod)) { |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 587 | static_key_set_entries(key, iter); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 588 | continue; |
| 589 | } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 590 | jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 591 | if (!jlm) |
| 592 | return -ENOMEM; |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 593 | if (!static_key_linked(key)) { |
| 594 | jlm2 = kzalloc(sizeof(struct static_key_mod), |
| 595 | GFP_KERNEL); |
| 596 | if (!jlm2) { |
| 597 | kfree(jlm); |
| 598 | return -ENOMEM; |
| 599 | } |
| 600 | preempt_disable(); |
| 601 | jlm2->mod = __module_address((unsigned long)key); |
| 602 | preempt_enable(); |
| 603 | jlm2->entries = static_key_entries(key); |
| 604 | jlm2->next = NULL; |
| 605 | static_key_set_mod(key, jlm2); |
| 606 | static_key_set_linked(key); |
| 607 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 608 | jlm->mod = mod; |
| 609 | jlm->entries = iter; |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 610 | jlm->next = static_key_mod(key); |
| 611 | static_key_set_mod(key, jlm); |
| 612 | static_key_set_linked(key); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 613 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 614 | /* Only update if we've changed from our initial state */ |
| 615 | if (jump_label_type(iter) != jump_label_init_type(iter)) |
Ard Biesheuvel | 1948367 | 2018-09-18 23:51:42 -0700 | [diff] [blame] | 616 | __jump_label_update(key, iter, iter_stop, true); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static void jump_label_del_module(struct module *mod) |
| 623 | { |
| 624 | struct jump_entry *iter_start = mod->jump_entries; |
| 625 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
| 626 | struct jump_entry *iter; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 627 | struct static_key *key = NULL; |
| 628 | struct static_key_mod *jlm, **prev; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 629 | |
| 630 | for (iter = iter_start; iter < iter_stop; iter++) { |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 631 | if (jump_entry_key(iter) == key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 632 | continue; |
| 633 | |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 634 | key = jump_entry_key(iter); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 635 | |
Ard Biesheuvel | 9ae033a | 2018-09-18 23:51:36 -0700 | [diff] [blame] | 636 | if (within_module((unsigned long)key, mod)) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 637 | continue; |
| 638 | |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 639 | /* No memory during module load */ |
| 640 | if (WARN_ON(!static_key_linked(key))) |
| 641 | continue; |
| 642 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 643 | prev = &key->next; |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 644 | jlm = static_key_mod(key); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 645 | |
| 646 | while (jlm && jlm->mod != mod) { |
| 647 | prev = &jlm->next; |
| 648 | jlm = jlm->next; |
| 649 | } |
| 650 | |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 651 | /* No memory during module load */ |
| 652 | if (WARN_ON(!jlm)) |
| 653 | continue; |
| 654 | |
| 655 | if (prev == &key->next) |
| 656 | static_key_set_mod(key, jlm->next); |
| 657 | else |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 658 | *prev = jlm->next; |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 659 | |
| 660 | kfree(jlm); |
| 661 | |
| 662 | jlm = static_key_mod(key); |
| 663 | /* if only one etry is left, fold it back into the static_key */ |
| 664 | if (jlm->next == NULL) { |
| 665 | static_key_set_entries(key, jlm->entries); |
| 666 | static_key_clear_linked(key); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 667 | kfree(jlm); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 672 | static int |
| 673 | jump_label_module_notify(struct notifier_block *self, unsigned long val, |
| 674 | void *data) |
| 675 | { |
| 676 | struct module *mod = data; |
| 677 | int ret = 0; |
| 678 | |
Thomas Gleixner | f2545b2d | 2017-05-24 10:15:35 +0200 | [diff] [blame] | 679 | cpus_read_lock(); |
| 680 | jump_label_lock(); |
| 681 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 682 | switch (val) { |
| 683 | case MODULE_STATE_COMING: |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 684 | ret = jump_label_add_module(mod); |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 685 | if (ret) { |
Borislav Petkov | da260fe | 2018-09-07 12:35:21 +0200 | [diff] [blame] | 686 | WARN(1, "Failed to allocate memory: jump_label may not work properly.\n"); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 687 | jump_label_del_module(mod); |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 688 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 689 | break; |
| 690 | case MODULE_STATE_GOING: |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 691 | jump_label_del_module(mod); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 692 | break; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 693 | } |
| 694 | |
Thomas Gleixner | f2545b2d | 2017-05-24 10:15:35 +0200 | [diff] [blame] | 695 | jump_label_unlock(); |
| 696 | cpus_read_unlock(); |
| 697 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 698 | return notifier_from_errno(ret); |
| 699 | } |
| 700 | |
Wei Yongjun | 885885f | 2016-06-17 17:19:40 +0000 | [diff] [blame] | 701 | static struct notifier_block jump_label_module_nb = { |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 702 | .notifier_call = jump_label_module_notify, |
| 703 | .priority = 1, /* higher than tracepoints */ |
| 704 | }; |
| 705 | |
| 706 | static __init int jump_label_init_module(void) |
| 707 | { |
| 708 | return register_module_notifier(&jump_label_module_nb); |
| 709 | } |
| 710 | early_initcall(jump_label_init_module); |
| 711 | |
| 712 | #endif /* CONFIG_MODULES */ |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 713 | |
| 714 | /*** |
| 715 | * jump_label_text_reserved - check if addr range is reserved |
| 716 | * @start: start text addr |
| 717 | * @end: end text addr |
| 718 | * |
| 719 | * checks if the text addr located between @start and @end |
| 720 | * overlaps with any of the jump label patch addresses. Code |
| 721 | * that wants to modify kernel text should first verify that |
| 722 | * it does not overlap with any of the jump label addresses. |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 723 | * Caller must hold jump_label_mutex. |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 724 | * |
| 725 | * returns 1 if there is an overlap, 0 otherwise |
| 726 | */ |
| 727 | int jump_label_text_reserved(void *start, void *end) |
| 728 | { |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 729 | int ret = __jump_label_text_reserved(__start___jump_table, |
| 730 | __stop___jump_table, start, end); |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 731 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 732 | if (ret) |
| 733 | return ret; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 734 | |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 735 | #ifdef CONFIG_MODULES |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 736 | ret = __jump_label_mod_text_reserved(start, end); |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 737 | #endif |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 738 | return ret; |
| 739 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 740 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 741 | static void jump_label_update(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 742 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 743 | struct jump_entry *stop = __stop___jump_table; |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 744 | struct jump_entry *entry; |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 745 | #ifdef CONFIG_MODULES |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 746 | struct module *mod; |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 747 | |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 748 | if (static_key_linked(key)) { |
| 749 | __jump_label_mod_update(key); |
| 750 | return; |
| 751 | } |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 752 | |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 753 | preempt_disable(); |
| 754 | mod = __module_address((unsigned long)key); |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 755 | if (mod) |
| 756 | stop = mod->jump_entries + mod->num_jump_entries; |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 757 | preempt_enable(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 758 | #endif |
Jason Baron | 3821fd3 | 2017-02-03 15:42:24 -0500 | [diff] [blame] | 759 | entry = static_key_entries(key); |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 760 | /* if there are no users, entry can be NULL */ |
| 761 | if (entry) |
Ard Biesheuvel | 1948367 | 2018-09-18 23:51:42 -0700 | [diff] [blame] | 762 | __jump_label_update(key, entry, stop, |
| 763 | system_state < SYSTEM_RUNNING); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 764 | } |
| 765 | |
Peter Zijlstra | 1987c94 | 2015-07-27 18:32:09 +0200 | [diff] [blame] | 766 | #ifdef CONFIG_STATIC_KEYS_SELFTEST |
| 767 | static DEFINE_STATIC_KEY_TRUE(sk_true); |
| 768 | static DEFINE_STATIC_KEY_FALSE(sk_false); |
| 769 | |
| 770 | static __init int jump_label_test(void) |
| 771 | { |
| 772 | int i; |
| 773 | |
| 774 | for (i = 0; i < 2; i++) { |
| 775 | WARN_ON(static_key_enabled(&sk_true.key) != true); |
| 776 | WARN_ON(static_key_enabled(&sk_false.key) != false); |
| 777 | |
| 778 | WARN_ON(!static_branch_likely(&sk_true)); |
| 779 | WARN_ON(!static_branch_unlikely(&sk_true)); |
| 780 | WARN_ON(static_branch_likely(&sk_false)); |
| 781 | WARN_ON(static_branch_unlikely(&sk_false)); |
| 782 | |
| 783 | static_branch_disable(&sk_true); |
| 784 | static_branch_enable(&sk_false); |
| 785 | |
| 786 | WARN_ON(static_key_enabled(&sk_true.key) == true); |
| 787 | WARN_ON(static_key_enabled(&sk_false.key) == false); |
| 788 | |
| 789 | WARN_ON(static_branch_likely(&sk_true)); |
| 790 | WARN_ON(static_branch_unlikely(&sk_true)); |
| 791 | WARN_ON(!static_branch_likely(&sk_false)); |
| 792 | WARN_ON(!static_branch_unlikely(&sk_false)); |
| 793 | |
| 794 | static_branch_enable(&sk_true); |
| 795 | static_branch_disable(&sk_false); |
| 796 | } |
| 797 | |
| 798 | return 0; |
| 799 | } |
Jason Baron | 92ee46e | 2017-11-13 16:48:47 -0500 | [diff] [blame] | 800 | early_initcall(jump_label_test); |
Peter Zijlstra | 1987c94 | 2015-07-27 18:32:09 +0200 | [diff] [blame] | 801 | #endif /* STATIC_KEYS_SELFTEST */ |