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 | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 17 | |
| 18 | #ifdef HAVE_JUMP_LABEL |
| 19 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 20 | /* mutex to protect coming/going of the the jump_label table */ |
| 21 | static DEFINE_MUTEX(jump_label_mutex); |
| 22 | |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 23 | void jump_label_lock(void) |
| 24 | { |
| 25 | mutex_lock(&jump_label_mutex); |
| 26 | } |
| 27 | |
| 28 | void jump_label_unlock(void) |
| 29 | { |
| 30 | mutex_unlock(&jump_label_mutex); |
| 31 | } |
| 32 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 33 | static int jump_label_cmp(const void *a, const void *b) |
| 34 | { |
| 35 | const struct jump_entry *jea = a; |
| 36 | const struct jump_entry *jeb = b; |
| 37 | |
| 38 | if (jea->key < jeb->key) |
| 39 | return -1; |
| 40 | |
| 41 | if (jea->key > jeb->key) |
| 42 | return 1; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static void |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 48 | jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 49 | { |
| 50 | unsigned long size; |
| 51 | |
| 52 | size = (((unsigned long)stop - (unsigned long)start) |
| 53 | / sizeof(struct jump_entry)); |
| 54 | sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL); |
| 55 | } |
| 56 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 57 | static void jump_label_update(struct static_key *key); |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 58 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 59 | void static_key_slow_inc(struct static_key *key) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 60 | { |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame^] | 61 | int v, v1; |
| 62 | |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 63 | STATIC_KEY_CHECK_USE(); |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame^] | 64 | |
| 65 | /* |
| 66 | * Careful if we get concurrent static_key_slow_inc() calls; |
| 67 | * later calls must wait for the first one to _finish_ the |
| 68 | * jump_label_update() process. At the same time, however, |
| 69 | * the jump_label_update() call below wants to see |
| 70 | * static_key_enabled(&key) for jumps to be updated properly. |
| 71 | * |
| 72 | * So give a special meaning to negative key->enabled: it sends |
| 73 | * static_key_slow_inc() down the slow path, and it is non-zero |
| 74 | * so it counts as "enabled" in jump_label_update(). Note that |
| 75 | * atomic_inc_unless_negative() checks >= 0, so roll our own. |
| 76 | */ |
| 77 | for (v = atomic_read(&key->enabled); v > 0; v = v1) { |
| 78 | v1 = atomic_cmpxchg(&key->enabled, v, v + 1); |
| 79 | if (likely(v1 == v)) |
| 80 | return; |
| 81 | } |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 82 | |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 83 | jump_label_lock(); |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame^] | 84 | if (atomic_read(&key->enabled) == 0) { |
| 85 | atomic_set(&key->enabled, -1); |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 86 | jump_label_update(key); |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame^] | 87 | atomic_set(&key->enabled, 1); |
| 88 | } else { |
| 89 | atomic_inc(&key->enabled); |
| 90 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 91 | jump_label_unlock(); |
| 92 | } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(static_key_slow_inc); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 94 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 95 | static void __static_key_slow_dec(struct static_key *key, |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 96 | unsigned long rate_limit, struct delayed_work *work) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 97 | { |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame^] | 98 | /* |
| 99 | * The negative count check is valid even when a negative |
| 100 | * key->enabled is in use by static_key_slow_inc(); a |
| 101 | * __static_key_slow_dec() before the first static_key_slow_inc() |
| 102 | * returns is unbalanced, because all other static_key_slow_inc() |
| 103 | * instances block while the update is in progress. |
| 104 | */ |
Jason Baron | fadf046 | 2012-02-21 15:02:53 -0500 | [diff] [blame] | 105 | if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) { |
| 106 | WARN(atomic_read(&key->enabled) < 0, |
| 107 | "jump label: negative count!\n"); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 108 | return; |
Jason Baron | fadf046 | 2012-02-21 15:02:53 -0500 | [diff] [blame] | 109 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 110 | |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 111 | if (rate_limit) { |
| 112 | atomic_inc(&key->enabled); |
| 113 | schedule_delayed_work(work, rate_limit); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 114 | } else { |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 115 | jump_label_update(key); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 116 | } |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 117 | jump_label_unlock(); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 120 | static void jump_label_update_timeout(struct work_struct *work) |
| 121 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 122 | struct static_key_deferred *key = |
| 123 | container_of(work, struct static_key_deferred, work.work); |
| 124 | __static_key_slow_dec(&key->key, 0, NULL); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 127 | void static_key_slow_dec(struct static_key *key) |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 128 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 129 | STATIC_KEY_CHECK_USE(); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 130 | __static_key_slow_dec(key, 0, NULL); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 131 | } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 132 | EXPORT_SYMBOL_GPL(static_key_slow_dec); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 133 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 134 | void static_key_slow_dec_deferred(struct static_key_deferred *key) |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 135 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 136 | STATIC_KEY_CHECK_USE(); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 137 | __static_key_slow_dec(&key->key, key->timeout, &key->work); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 138 | } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 139 | EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 140 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 141 | void jump_label_rate_limit(struct static_key_deferred *key, |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 142 | unsigned long rl) |
| 143 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 144 | STATIC_KEY_CHECK_USE(); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 145 | key->timeout = rl; |
| 146 | INIT_DELAYED_WORK(&key->work, jump_label_update_timeout); |
| 147 | } |
Gleb Natapov | a181dc1 | 2012-08-05 15:58:29 +0300 | [diff] [blame] | 148 | EXPORT_SYMBOL_GPL(jump_label_rate_limit); |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 149 | |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 150 | static int addr_conflict(struct jump_entry *entry, void *start, void *end) |
| 151 | { |
| 152 | if (entry->code <= (unsigned long)end && |
| 153 | entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start) |
| 154 | return 1; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 159 | static int __jump_label_text_reserved(struct jump_entry *iter_start, |
| 160 | struct jump_entry *iter_stop, void *start, void *end) |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 161 | { |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 162 | struct jump_entry *iter; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 163 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 164 | iter = iter_start; |
| 165 | while (iter < iter_stop) { |
| 166 | if (addr_conflict(iter, start, end)) |
| 167 | return 1; |
| 168 | iter++; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 169 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 170 | |
| 171 | return 0; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 174 | /* |
Jeremy Fitzhardinge | 20284aa | 2011-10-03 11:01:46 -0700 | [diff] [blame] | 175 | * Update code which is definitely not currently executing. |
| 176 | * Architectures which need heavyweight synchronization to modify |
| 177 | * running code can override this to make the non-live update case |
| 178 | * cheaper. |
| 179 | */ |
Peter Zijlstra | 9cdbe1c | 2011-12-06 17:27:29 +0100 | [diff] [blame] | 180 | 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] | 181 | enum jump_label_type type) |
| 182 | { |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 183 | arch_jump_label_transform(entry, type); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 186 | static inline struct jump_entry *static_key_entries(struct static_key *key) |
| 187 | { |
| 188 | return (struct jump_entry *)((unsigned long)key->entries & ~JUMP_TYPE_MASK); |
| 189 | } |
| 190 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 191 | static inline bool static_key_type(struct static_key *key) |
| 192 | { |
| 193 | return (unsigned long)key->entries & JUMP_TYPE_MASK; |
| 194 | } |
| 195 | |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 196 | static inline struct static_key *jump_entry_key(struct jump_entry *entry) |
| 197 | { |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 198 | return (struct static_key *)((unsigned long)entry->key & ~1UL); |
| 199 | } |
| 200 | |
| 201 | static bool jump_entry_branch(struct jump_entry *entry) |
| 202 | { |
| 203 | return (unsigned long)entry->key & 1UL; |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 206 | static enum jump_label_type jump_label_type(struct jump_entry *entry) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 207 | { |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 208 | struct static_key *key = jump_entry_key(entry); |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 209 | bool enabled = static_key_enabled(key); |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 210 | bool branch = jump_entry_branch(entry); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 211 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 212 | /* See the comment in linux/jump_label.h */ |
| 213 | return enabled ^ branch; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 216 | static void __jump_label_update(struct static_key *key, |
| 217 | struct jump_entry *entry, |
| 218 | struct jump_entry *stop) |
| 219 | { |
| 220 | for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) { |
| 221 | /* |
| 222 | * entry->code set to 0 invalidates module init text sections |
| 223 | * kernel_text_address() verifies we are not in core kernel |
| 224 | * init code, see jump_label_invalidate_module_init(). |
| 225 | */ |
| 226 | if (entry->code && kernel_text_address(entry->code)) |
| 227 | arch_jump_label_transform(entry, jump_label_type(entry)); |
| 228 | } |
| 229 | } |
| 230 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 231 | void __init jump_label_init(void) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 232 | { |
| 233 | struct jump_entry *iter_start = __start___jump_table; |
| 234 | struct jump_entry *iter_stop = __stop___jump_table; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 235 | struct static_key *key = NULL; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 236 | struct jump_entry *iter; |
| 237 | |
| 238 | jump_label_lock(); |
| 239 | jump_label_sort_entries(iter_start, iter_stop); |
| 240 | |
| 241 | for (iter = iter_start; iter < iter_stop; iter++) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 242 | struct static_key *iterk; |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 243 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 244 | /* rewrite NOPs */ |
| 245 | if (jump_label_type(iter) == JUMP_LABEL_NOP) |
| 246 | arch_jump_label_transform_static(iter, JUMP_LABEL_NOP); |
| 247 | |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 248 | iterk = jump_entry_key(iter); |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 249 | if (iterk == key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 250 | continue; |
| 251 | |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 252 | key = iterk; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 253 | /* |
| 254 | * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH. |
| 255 | */ |
| 256 | *((unsigned long *)&key->entries) += (unsigned long)iter; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 257 | #ifdef CONFIG_MODULES |
| 258 | key->next = NULL; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 259 | #endif |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 260 | } |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 261 | static_key_initialized = true; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 262 | jump_label_unlock(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 263 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 264 | |
| 265 | #ifdef CONFIG_MODULES |
| 266 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 267 | static enum jump_label_type jump_label_init_type(struct jump_entry *entry) |
| 268 | { |
| 269 | struct static_key *key = jump_entry_key(entry); |
| 270 | bool type = static_key_type(key); |
| 271 | bool branch = jump_entry_branch(entry); |
| 272 | |
| 273 | /* See the comment in linux/jump_label.h */ |
| 274 | return type ^ branch; |
| 275 | } |
| 276 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 277 | struct static_key_mod { |
| 278 | struct static_key_mod *next; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 279 | struct jump_entry *entries; |
| 280 | struct module *mod; |
| 281 | }; |
| 282 | |
| 283 | static int __jump_label_mod_text_reserved(void *start, void *end) |
| 284 | { |
| 285 | struct module *mod; |
| 286 | |
| 287 | mod = __module_text_address((unsigned long)start); |
| 288 | if (!mod) |
| 289 | return 0; |
| 290 | |
| 291 | WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod); |
| 292 | |
| 293 | return __jump_label_text_reserved(mod->jump_entries, |
| 294 | mod->jump_entries + mod->num_jump_entries, |
| 295 | start, end); |
| 296 | } |
| 297 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 298 | static void __jump_label_mod_update(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 299 | { |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 300 | struct static_key_mod *mod; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 301 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 302 | for (mod = key->next; mod; mod = mod->next) { |
Jiri Olsa | 7cbc5b8 | 2011-05-10 12:43:46 +0200 | [diff] [blame] | 303 | struct module *m = mod->mod; |
| 304 | |
| 305 | __jump_label_update(key, mod->entries, |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 306 | m->jump_entries + m->num_jump_entries); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | /*** |
| 311 | * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop() |
| 312 | * @mod: module to patch |
| 313 | * |
| 314 | * Allow for run-time selection of the optimal nops. Before the module |
| 315 | * loads patch these with arch_get_jump_label_nop(), which is specified by |
| 316 | * the arch specific jump label code. |
| 317 | */ |
| 318 | void jump_label_apply_nops(struct module *mod) |
| 319 | { |
| 320 | struct jump_entry *iter_start = mod->jump_entries; |
| 321 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
| 322 | struct jump_entry *iter; |
| 323 | |
| 324 | /* if the module doesn't have jump label entries, just return */ |
| 325 | if (iter_start == iter_stop) |
| 326 | return; |
| 327 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 328 | for (iter = iter_start; iter < iter_stop; iter++) { |
| 329 | /* Only write NOPs for arch_branch_static(). */ |
| 330 | if (jump_label_init_type(iter) == JUMP_LABEL_NOP) |
| 331 | arch_jump_label_transform_static(iter, JUMP_LABEL_NOP); |
| 332 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int jump_label_add_module(struct module *mod) |
| 336 | { |
| 337 | struct jump_entry *iter_start = mod->jump_entries; |
| 338 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
| 339 | struct jump_entry *iter; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 340 | struct static_key *key = NULL; |
| 341 | struct static_key_mod *jlm; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 342 | |
| 343 | /* if the module doesn't have jump label entries, just return */ |
| 344 | if (iter_start == iter_stop) |
| 345 | return 0; |
| 346 | |
| 347 | jump_label_sort_entries(iter_start, iter_stop); |
| 348 | |
| 349 | for (iter = iter_start; iter < iter_stop; iter++) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 350 | struct static_key *iterk; |
| 351 | |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 352 | iterk = jump_entry_key(iter); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 353 | if (iterk == key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 354 | continue; |
| 355 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 356 | key = iterk; |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 357 | if (within_module(iter->key, mod)) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 358 | /* |
| 359 | * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH. |
| 360 | */ |
| 361 | *((unsigned long *)&key->entries) += (unsigned long)iter; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 362 | key->next = NULL; |
| 363 | continue; |
| 364 | } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 365 | jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 366 | if (!jlm) |
| 367 | return -ENOMEM; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 368 | jlm->mod = mod; |
| 369 | jlm->entries = iter; |
| 370 | jlm->next = key->next; |
| 371 | key->next = jlm; |
| 372 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 373 | /* Only update if we've changed from our initial state */ |
| 374 | if (jump_label_type(iter) != jump_label_init_type(iter)) |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 375 | __jump_label_update(key, iter, iter_stop); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void jump_label_del_module(struct module *mod) |
| 382 | { |
| 383 | struct jump_entry *iter_start = mod->jump_entries; |
| 384 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
| 385 | struct jump_entry *iter; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 386 | struct static_key *key = NULL; |
| 387 | struct static_key_mod *jlm, **prev; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 388 | |
| 389 | for (iter = iter_start; iter < iter_stop; iter++) { |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 390 | if (jump_entry_key(iter) == key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 391 | continue; |
| 392 | |
Peter Zijlstra | 7dcfd91 | 2015-07-24 15:02:27 +0200 | [diff] [blame] | 393 | key = jump_entry_key(iter); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 394 | |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 395 | if (within_module(iter->key, mod)) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 396 | continue; |
| 397 | |
| 398 | prev = &key->next; |
| 399 | jlm = key->next; |
| 400 | |
| 401 | while (jlm && jlm->mod != mod) { |
| 402 | prev = &jlm->next; |
| 403 | jlm = jlm->next; |
| 404 | } |
| 405 | |
| 406 | if (jlm) { |
| 407 | *prev = jlm->next; |
| 408 | kfree(jlm); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static void jump_label_invalidate_module_init(struct module *mod) |
| 414 | { |
| 415 | struct jump_entry *iter_start = mod->jump_entries; |
| 416 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
| 417 | struct jump_entry *iter; |
| 418 | |
| 419 | for (iter = iter_start; iter < iter_stop; iter++) { |
| 420 | if (within_module_init(iter->code, mod)) |
| 421 | iter->code = 0; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static int |
| 426 | jump_label_module_notify(struct notifier_block *self, unsigned long val, |
| 427 | void *data) |
| 428 | { |
| 429 | struct module *mod = data; |
| 430 | int ret = 0; |
| 431 | |
| 432 | switch (val) { |
| 433 | case MODULE_STATE_COMING: |
| 434 | jump_label_lock(); |
| 435 | ret = jump_label_add_module(mod); |
| 436 | if (ret) |
| 437 | jump_label_del_module(mod); |
| 438 | jump_label_unlock(); |
| 439 | break; |
| 440 | case MODULE_STATE_GOING: |
| 441 | jump_label_lock(); |
| 442 | jump_label_del_module(mod); |
| 443 | jump_label_unlock(); |
| 444 | break; |
| 445 | case MODULE_STATE_LIVE: |
| 446 | jump_label_lock(); |
| 447 | jump_label_invalidate_module_init(mod); |
| 448 | jump_label_unlock(); |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | return notifier_from_errno(ret); |
| 453 | } |
| 454 | |
| 455 | struct notifier_block jump_label_module_nb = { |
| 456 | .notifier_call = jump_label_module_notify, |
| 457 | .priority = 1, /* higher than tracepoints */ |
| 458 | }; |
| 459 | |
| 460 | static __init int jump_label_init_module(void) |
| 461 | { |
| 462 | return register_module_notifier(&jump_label_module_nb); |
| 463 | } |
| 464 | early_initcall(jump_label_init_module); |
| 465 | |
| 466 | #endif /* CONFIG_MODULES */ |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 467 | |
| 468 | /*** |
| 469 | * jump_label_text_reserved - check if addr range is reserved |
| 470 | * @start: start text addr |
| 471 | * @end: end text addr |
| 472 | * |
| 473 | * checks if the text addr located between @start and @end |
| 474 | * overlaps with any of the jump label patch addresses. Code |
| 475 | * that wants to modify kernel text should first verify that |
| 476 | * it does not overlap with any of the jump label addresses. |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 477 | * Caller must hold jump_label_mutex. |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 478 | * |
| 479 | * returns 1 if there is an overlap, 0 otherwise |
| 480 | */ |
| 481 | int jump_label_text_reserved(void *start, void *end) |
| 482 | { |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 483 | int ret = __jump_label_text_reserved(__start___jump_table, |
| 484 | __stop___jump_table, start, end); |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 485 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 486 | if (ret) |
| 487 | return ret; |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 488 | |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 489 | #ifdef CONFIG_MODULES |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 490 | ret = __jump_label_mod_text_reserved(start, end); |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 491 | #endif |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 492 | return ret; |
| 493 | } |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 494 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 495 | static void jump_label_update(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 496 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 497 | struct jump_entry *stop = __stop___jump_table; |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 498 | struct jump_entry *entry = static_key_entries(key); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 499 | #ifdef CONFIG_MODULES |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 500 | struct module *mod; |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 501 | |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 502 | __jump_label_mod_update(key); |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 503 | |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 504 | preempt_disable(); |
| 505 | mod = __module_address((unsigned long)key); |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 506 | if (mod) |
| 507 | stop = mod->jump_entries + mod->num_jump_entries; |
Peter Zijlstra | bed831f | 2015-05-27 11:09:35 +0930 | [diff] [blame] | 508 | preempt_enable(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 509 | #endif |
Xiao Guangrong | 140fe3b | 2011-06-21 10:35:55 +0800 | [diff] [blame] | 510 | /* if there are no users, entry can be NULL */ |
| 511 | if (entry) |
Peter Zijlstra | 706249c | 2015-07-24 15:06:37 +0200 | [diff] [blame] | 512 | __jump_label_update(key, entry, stop); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Peter Zijlstra | 1987c94 | 2015-07-27 18:32:09 +0200 | [diff] [blame] | 515 | #ifdef CONFIG_STATIC_KEYS_SELFTEST |
| 516 | static DEFINE_STATIC_KEY_TRUE(sk_true); |
| 517 | static DEFINE_STATIC_KEY_FALSE(sk_false); |
| 518 | |
| 519 | static __init int jump_label_test(void) |
| 520 | { |
| 521 | int i; |
| 522 | |
| 523 | for (i = 0; i < 2; i++) { |
| 524 | WARN_ON(static_key_enabled(&sk_true.key) != true); |
| 525 | WARN_ON(static_key_enabled(&sk_false.key) != false); |
| 526 | |
| 527 | WARN_ON(!static_branch_likely(&sk_true)); |
| 528 | WARN_ON(!static_branch_unlikely(&sk_true)); |
| 529 | WARN_ON(static_branch_likely(&sk_false)); |
| 530 | WARN_ON(static_branch_unlikely(&sk_false)); |
| 531 | |
| 532 | static_branch_disable(&sk_true); |
| 533 | static_branch_enable(&sk_false); |
| 534 | |
| 535 | WARN_ON(static_key_enabled(&sk_true.key) == true); |
| 536 | WARN_ON(static_key_enabled(&sk_false.key) == false); |
| 537 | |
| 538 | WARN_ON(static_branch_likely(&sk_true)); |
| 539 | WARN_ON(static_branch_unlikely(&sk_true)); |
| 540 | WARN_ON(!static_branch_likely(&sk_false)); |
| 541 | WARN_ON(!static_branch_unlikely(&sk_false)); |
| 542 | |
| 543 | static_branch_enable(&sk_true); |
| 544 | static_branch_disable(&sk_false); |
| 545 | } |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | late_initcall(jump_label_test); |
| 550 | #endif /* STATIC_KEYS_SELFTEST */ |
| 551 | |
| 552 | #endif /* HAVE_JUMP_LABEL */ |