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