blob: cc6d815c75ed5fbfc1fecf1592d3dc4b3133f94d [file] [log] [blame]
Jason Baronbf5438fc2010-09-17 11:09:00 -04001/*
2 * jump label support
3 *
4 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
Peter Zijlstra90eec102015-11-16 11:08:45 +01005 * Copyright (C) 2011 Peter Zijlstra
Jason Baronbf5438fc2010-09-17 11:09:00 -04006 *
7 */
Jason Baronbf5438fc2010-09-17 11:09:00 -04008#include <linux/memory.h>
9#include <linux/uaccess.h>
10#include <linux/module.h>
11#include <linux/list.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040012#include <linux/slab.h>
13#include <linux/sort.h>
14#include <linux/err.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +010015#include <linux/static_key.h>
Andrew Jones851cf6e2013-08-09 19:51:57 +053016#include <linux/jump_label_ratelimit.h>
Jason Baron1f69bf92016-08-03 13:46:36 -070017#include <linux/bug.h>
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +020018#include <linux/cpu.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040019
20#ifdef HAVE_JUMP_LABEL
21
Jason Baronbf5438fc2010-09-17 11:09:00 -040022/* mutex to protect coming/going of the the jump_label table */
23static DEFINE_MUTEX(jump_label_mutex);
24
Jason Baron91bad2f2010-10-01 17:23:48 -040025void jump_label_lock(void)
26{
27 mutex_lock(&jump_label_mutex);
28}
29
30void jump_label_unlock(void)
31{
32 mutex_unlock(&jump_label_mutex);
33}
34
Jason Baronbf5438fc2010-09-17 11:09:00 -040035static 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
49static void
Jason Barond430d3d2011-03-16 17:29:47 -040050jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
Jason Baronbf5438fc2010-09-17 11:09:00 -040051{
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 Zijlstra706249c2015-07-24 15:06:37 +020059static void jump_label_update(struct static_key *key);
Peter Zijlstraa1efb012015-07-24 14:55:40 +020060
Jason Baron1f69bf92016-08-03 13:46:36 -070061/*
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 */
70int 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}
80EXPORT_SYMBOL_GPL(static_key_count);
81
Marc Zyngier8b7b4122017-08-01 09:02:55 +010082static void static_key_slow_inc_cpuslocked(struct static_key *key)
Jason Baronbf5438fc2010-09-17 11:09:00 -040083{
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +020084 int v, v1;
85
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +020086 STATIC_KEY_CHECK_USE();
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +020087
88 /*
89 * Careful if we get concurrent static_key_slow_inc() calls;
90 * later calls must wait for the first one to _finish_ the
91 * jump_label_update() process. At the same time, however,
92 * the jump_label_update() call below wants to see
93 * static_key_enabled(&key) for jumps to be updated properly.
94 *
95 * So give a special meaning to negative key->enabled: it sends
96 * static_key_slow_inc() down the slow path, and it is non-zero
97 * so it counts as "enabled" in jump_label_update(). Note that
98 * atomic_inc_unless_negative() checks >= 0, so roll our own.
99 */
100 for (v = atomic_read(&key->enabled); v > 0; v = v1) {
101 v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
Marc Zyngier8b7b4122017-08-01 09:02:55 +0100102 if (likely(v1 == v))
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200103 return;
104 }
Jason Baronbf5438fc2010-09-17 11:09:00 -0400105
Jason Baron91bad2f2010-10-01 17:23:48 -0400106 jump_label_lock();
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200107 if (atomic_read(&key->enabled) == 0) {
108 atomic_set(&key->enabled, -1);
Peter Zijlstra706249c2015-07-24 15:06:37 +0200109 jump_label_update(key);
Peter Zijlstrad0646a62017-08-01 23:58:50 +0200110 /*
111 * Ensure that if the above cmpxchg loop observes our positive
112 * value, it must also observe all the text changes.
113 */
114 atomic_set_release(&key->enabled, 1);
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200115 } else {
116 atomic_inc(&key->enabled);
117 }
Jason Barond430d3d2011-03-16 17:29:47 -0400118 jump_label_unlock();
Marc Zyngier8b7b4122017-08-01 09:02:55 +0100119}
120
121void static_key_slow_inc(struct static_key *key)
122{
123 cpus_read_lock();
124 static_key_slow_inc_cpuslocked(key);
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200125 cpus_read_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400126}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100127EXPORT_SYMBOL_GPL(static_key_slow_inc);
Jason Barond430d3d2011-03-16 17:29:47 -0400128
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200129void static_key_enable(struct static_key *key)
130{
131 STATIC_KEY_CHECK_USE();
132 if (atomic_read(&key->enabled) > 0) {
133 WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
134 return;
135 }
136
137 cpus_read_lock();
138 jump_label_lock();
139 if (atomic_read(&key->enabled) == 0) {
140 atomic_set(&key->enabled, -1);
141 jump_label_update(key);
Peter Zijlstrad0646a62017-08-01 23:58:50 +0200142 /*
143 * See static_key_slow_inc().
144 */
145 atomic_set_release(&key->enabled, 1);
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200146 }
147 jump_label_unlock();
148 cpus_read_unlock();
149}
150EXPORT_SYMBOL_GPL(static_key_enable);
151
152void static_key_disable(struct static_key *key)
153{
154 STATIC_KEY_CHECK_USE();
155 if (atomic_read(&key->enabled) != 1) {
156 WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
157 return;
158 }
159
160 cpus_read_lock();
161 jump_label_lock();
162 if (atomic_cmpxchg(&key->enabled, 1, 0))
163 jump_label_update(key);
164 jump_label_unlock();
165 cpus_read_unlock();
166}
167EXPORT_SYMBOL_GPL(static_key_disable);
168
Marc Zyngier8b7b4122017-08-01 09:02:55 +0100169static void static_key_slow_dec_cpuslocked(struct static_key *key,
170 unsigned long rate_limit,
171 struct delayed_work *work)
Jason Barond430d3d2011-03-16 17:29:47 -0400172{
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200173 /*
174 * The negative count check is valid even when a negative
175 * key->enabled is in use by static_key_slow_inc(); a
176 * __static_key_slow_dec() before the first static_key_slow_inc()
177 * returns is unbalanced, because all other static_key_slow_inc()
178 * instances block while the update is in progress.
179 */
Jason Baronfadf0462012-02-21 15:02:53 -0500180 if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
181 WARN(atomic_read(&key->enabled) < 0,
182 "jump label: negative count!\n");
Jason Barond430d3d2011-03-16 17:29:47 -0400183 return;
Jason Baronfadf0462012-02-21 15:02:53 -0500184 }
Jason Barond430d3d2011-03-16 17:29:47 -0400185
Gleb Natapovb2029522011-11-27 17:59:09 +0200186 if (rate_limit) {
187 atomic_inc(&key->enabled);
188 schedule_delayed_work(work, rate_limit);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100189 } else {
Peter Zijlstra706249c2015-07-24 15:06:37 +0200190 jump_label_update(key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100191 }
Jason Baron91bad2f2010-10-01 17:23:48 -0400192 jump_label_unlock();
Marc Zyngier8b7b4122017-08-01 09:02:55 +0100193}
194
195static void __static_key_slow_dec(struct static_key *key,
196 unsigned long rate_limit,
197 struct delayed_work *work)
198{
199 cpus_read_lock();
200 static_key_slow_dec_cpuslocked(key, rate_limit, work);
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200201 cpus_read_unlock();
Jason Baronbf5438fc2010-09-17 11:09:00 -0400202}
203
Gleb Natapovb2029522011-11-27 17:59:09 +0200204static void jump_label_update_timeout(struct work_struct *work)
205{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100206 struct static_key_deferred *key =
207 container_of(work, struct static_key_deferred, work.work);
208 __static_key_slow_dec(&key->key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200209}
210
Ingo Molnarc5905af2012-02-24 08:31:31 +0100211void static_key_slow_dec(struct static_key *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200212{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200213 STATIC_KEY_CHECK_USE();
Ingo Molnarc5905af2012-02-24 08:31:31 +0100214 __static_key_slow_dec(key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200215}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100216EXPORT_SYMBOL_GPL(static_key_slow_dec);
Gleb Natapovb2029522011-11-27 17:59:09 +0200217
Ingo Molnarc5905af2012-02-24 08:31:31 +0100218void static_key_slow_dec_deferred(struct static_key_deferred *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200219{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200220 STATIC_KEY_CHECK_USE();
Ingo Molnarc5905af2012-02-24 08:31:31 +0100221 __static_key_slow_dec(&key->key, key->timeout, &key->work);
Gleb Natapovb2029522011-11-27 17:59:09 +0200222}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100223EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
Gleb Natapovb2029522011-11-27 17:59:09 +0200224
David Matlackb6416e62016-12-16 14:30:35 -0800225void static_key_deferred_flush(struct static_key_deferred *key)
226{
227 STATIC_KEY_CHECK_USE();
228 flush_delayed_work(&key->work);
229}
230EXPORT_SYMBOL_GPL(static_key_deferred_flush);
231
Ingo Molnarc5905af2012-02-24 08:31:31 +0100232void jump_label_rate_limit(struct static_key_deferred *key,
Gleb Natapovb2029522011-11-27 17:59:09 +0200233 unsigned long rl)
234{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200235 STATIC_KEY_CHECK_USE();
Gleb Natapovb2029522011-11-27 17:59:09 +0200236 key->timeout = rl;
237 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
238}
Gleb Natapova181dc12012-08-05 15:58:29 +0300239EXPORT_SYMBOL_GPL(jump_label_rate_limit);
Gleb Natapovb2029522011-11-27 17:59:09 +0200240
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400241static int addr_conflict(struct jump_entry *entry, void *start, void *end)
242{
243 if (entry->code <= (unsigned long)end &&
244 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
245 return 1;
246
247 return 0;
248}
249
Jason Barond430d3d2011-03-16 17:29:47 -0400250static int __jump_label_text_reserved(struct jump_entry *iter_start,
251 struct jump_entry *iter_stop, void *start, void *end)
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400252{
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400253 struct jump_entry *iter;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400254
Jason Barond430d3d2011-03-16 17:29:47 -0400255 iter = iter_start;
256 while (iter < iter_stop) {
257 if (addr_conflict(iter, start, end))
258 return 1;
259 iter++;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400260 }
Jason Barond430d3d2011-03-16 17:29:47 -0400261
262 return 0;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400263}
264
Peter Zijlstra706249c2015-07-24 15:06:37 +0200265/*
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700266 * Update code which is definitely not currently executing.
267 * Architectures which need heavyweight synchronization to modify
268 * running code can override this to make the non-live update case
269 * cheaper.
270 */
Peter Zijlstra9cdbe1c2011-12-06 17:27:29 +0100271void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700272 enum jump_label_type type)
273{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200274 arch_jump_label_transform(entry, type);
Jason Barond430d3d2011-03-16 17:29:47 -0400275}
276
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200277static inline struct jump_entry *static_key_entries(struct static_key *key)
278{
Jason Baron3821fd32017-02-03 15:42:24 -0500279 WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
280 return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200281}
282
Peter Zijlstra706249c2015-07-24 15:06:37 +0200283static inline bool static_key_type(struct static_key *key)
284{
Jason Baron3821fd32017-02-03 15:42:24 -0500285 return key->type & JUMP_TYPE_TRUE;
286}
287
288static inline bool static_key_linked(struct static_key *key)
289{
290 return key->type & JUMP_TYPE_LINKED;
291}
292
293static inline void static_key_clear_linked(struct static_key *key)
294{
295 key->type &= ~JUMP_TYPE_LINKED;
296}
297
298static inline void static_key_set_linked(struct static_key *key)
299{
300 key->type |= JUMP_TYPE_LINKED;
Peter Zijlstra706249c2015-07-24 15:06:37 +0200301}
302
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200303static inline struct static_key *jump_entry_key(struct jump_entry *entry)
304{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200305 return (struct static_key *)((unsigned long)entry->key & ~1UL);
306}
307
308static bool jump_entry_branch(struct jump_entry *entry)
309{
310 return (unsigned long)entry->key & 1UL;
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200311}
312
Jason Baron3821fd32017-02-03 15:42:24 -0500313/***
314 * A 'struct static_key' uses a union such that it either points directly
315 * to a table of 'struct jump_entry' or to a linked list of modules which in
316 * turn point to 'struct jump_entry' tables.
317 *
318 * The two lower bits of the pointer are used to keep track of which pointer
319 * type is in use and to store the initial branch direction, we use an access
320 * function which preserves these bits.
321 */
322static void static_key_set_entries(struct static_key *key,
323 struct jump_entry *entries)
324{
325 unsigned long type;
326
327 WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
328 type = key->type & JUMP_TYPE_MASK;
329 key->entries = entries;
330 key->type |= type;
331}
332
Peter Zijlstra706249c2015-07-24 15:06:37 +0200333static enum jump_label_type jump_label_type(struct jump_entry *entry)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100334{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200335 struct static_key *key = jump_entry_key(entry);
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200336 bool enabled = static_key_enabled(key);
Peter Zijlstra11276d52015-07-24 15:09:55 +0200337 bool branch = jump_entry_branch(entry);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100338
Peter Zijlstra11276d52015-07-24 15:09:55 +0200339 /* See the comment in linux/jump_label.h */
340 return enabled ^ branch;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100341}
342
Peter Zijlstra706249c2015-07-24 15:06:37 +0200343static void __jump_label_update(struct static_key *key,
344 struct jump_entry *entry,
345 struct jump_entry *stop)
346{
347 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
348 /*
349 * entry->code set to 0 invalidates module init text sections
350 * kernel_text_address() verifies we are not in core kernel
351 * init code, see jump_label_invalidate_module_init().
352 */
353 if (entry->code && kernel_text_address(entry->code))
354 arch_jump_label_transform(entry, jump_label_type(entry));
355 }
356}
357
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700358void __init jump_label_init(void)
Jason Barond430d3d2011-03-16 17:29:47 -0400359{
360 struct jump_entry *iter_start = __start___jump_table;
361 struct jump_entry *iter_stop = __stop___jump_table;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100362 struct static_key *key = NULL;
Jason Barond430d3d2011-03-16 17:29:47 -0400363 struct jump_entry *iter;
364
Jason Baron1f69bf92016-08-03 13:46:36 -0700365 /*
366 * Since we are initializing the static_key.enabled field with
367 * with the 'raw' int values (to avoid pulling in atomic.h) in
368 * jump_label.h, let's make sure that is safe. There are only two
369 * cases to check since we initialize to 0 or 1.
370 */
371 BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
372 BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
373
Kevin Haoe3f91082016-07-23 14:42:37 +0530374 if (static_key_initialized)
375 return;
376
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200377 cpus_read_lock();
Jason Barond430d3d2011-03-16 17:29:47 -0400378 jump_label_lock();
379 jump_label_sort_entries(iter_start, iter_stop);
380
381 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100382 struct static_key *iterk;
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700383
Peter Zijlstra11276d52015-07-24 15:09:55 +0200384 /* rewrite NOPs */
385 if (jump_label_type(iter) == JUMP_LABEL_NOP)
386 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
387
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200388 iterk = jump_entry_key(iter);
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700389 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400390 continue;
391
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700392 key = iterk;
Jason Baron3821fd32017-02-03 15:42:24 -0500393 static_key_set_entries(key, iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400394 }
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200395 static_key_initialized = true;
Jason Barond430d3d2011-03-16 17:29:47 -0400396 jump_label_unlock();
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200397 cpus_read_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400398}
Jason Barond430d3d2011-03-16 17:29:47 -0400399
400#ifdef CONFIG_MODULES
401
Peter Zijlstra11276d52015-07-24 15:09:55 +0200402static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
403{
404 struct static_key *key = jump_entry_key(entry);
405 bool type = static_key_type(key);
406 bool branch = jump_entry_branch(entry);
407
408 /* See the comment in linux/jump_label.h */
409 return type ^ branch;
410}
411
Ingo Molnarc5905af2012-02-24 08:31:31 +0100412struct static_key_mod {
413 struct static_key_mod *next;
Jason Barond430d3d2011-03-16 17:29:47 -0400414 struct jump_entry *entries;
415 struct module *mod;
416};
417
Jason Baron3821fd32017-02-03 15:42:24 -0500418static inline struct static_key_mod *static_key_mod(struct static_key *key)
419{
420 WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
421 return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
422}
423
424/***
425 * key->type and key->next are the same via union.
426 * This sets key->next and preserves the type bits.
427 *
428 * See additional comments above static_key_set_entries().
429 */
430static void static_key_set_mod(struct static_key *key,
431 struct static_key_mod *mod)
432{
433 unsigned long type;
434
435 WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
436 type = key->type & JUMP_TYPE_MASK;
437 key->next = mod;
438 key->type |= type;
439}
440
Jason Barond430d3d2011-03-16 17:29:47 -0400441static int __jump_label_mod_text_reserved(void *start, void *end)
442{
443 struct module *mod;
444
Rusty Russellbdc9f372016-07-27 12:17:35 +0930445 preempt_disable();
Jason Barond430d3d2011-03-16 17:29:47 -0400446 mod = __module_text_address((unsigned long)start);
Rusty Russellbdc9f372016-07-27 12:17:35 +0930447 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
448 preempt_enable();
449
Jason Barond430d3d2011-03-16 17:29:47 -0400450 if (!mod)
451 return 0;
452
Jason Barond430d3d2011-03-16 17:29:47 -0400453
454 return __jump_label_text_reserved(mod->jump_entries,
455 mod->jump_entries + mod->num_jump_entries,
456 start, end);
457}
458
Peter Zijlstra706249c2015-07-24 15:06:37 +0200459static void __jump_label_mod_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400460{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200461 struct static_key_mod *mod;
Jason Barond430d3d2011-03-16 17:29:47 -0400462
Jason Baron3821fd32017-02-03 15:42:24 -0500463 for (mod = static_key_mod(key); mod; mod = mod->next) {
464 struct jump_entry *stop;
465 struct module *m;
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200466
Jason Baron3821fd32017-02-03 15:42:24 -0500467 /*
468 * NULL if the static_key is defined in a module
469 * that does not use it
470 */
471 if (!mod->entries)
472 continue;
473
474 m = mod->mod;
475 if (!m)
476 stop = __stop___jump_table;
477 else
478 stop = m->jump_entries + m->num_jump_entries;
479 __jump_label_update(key, mod->entries, stop);
Jason Barond430d3d2011-03-16 17:29:47 -0400480 }
481}
482
483/***
484 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
485 * @mod: module to patch
486 *
487 * Allow for run-time selection of the optimal nops. Before the module
488 * loads patch these with arch_get_jump_label_nop(), which is specified by
489 * the arch specific jump label code.
490 */
491void jump_label_apply_nops(struct module *mod)
492{
493 struct jump_entry *iter_start = mod->jump_entries;
494 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
495 struct jump_entry *iter;
496
497 /* if the module doesn't have jump label entries, just return */
498 if (iter_start == iter_stop)
499 return;
500
Peter Zijlstra11276d52015-07-24 15:09:55 +0200501 for (iter = iter_start; iter < iter_stop; iter++) {
502 /* Only write NOPs for arch_branch_static(). */
503 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
504 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
505 }
Jason Barond430d3d2011-03-16 17:29:47 -0400506}
507
508static int jump_label_add_module(struct module *mod)
509{
510 struct jump_entry *iter_start = mod->jump_entries;
511 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
512 struct jump_entry *iter;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100513 struct static_key *key = NULL;
Jason Baron3821fd32017-02-03 15:42:24 -0500514 struct static_key_mod *jlm, *jlm2;
Jason Barond430d3d2011-03-16 17:29:47 -0400515
516 /* if the module doesn't have jump label entries, just return */
517 if (iter_start == iter_stop)
518 return 0;
519
520 jump_label_sort_entries(iter_start, iter_stop);
521
522 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100523 struct static_key *iterk;
524
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200525 iterk = jump_entry_key(iter);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100526 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400527 continue;
528
Ingo Molnarc5905af2012-02-24 08:31:31 +0100529 key = iterk;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930530 if (within_module(iter->key, mod)) {
Jason Baron3821fd32017-02-03 15:42:24 -0500531 static_key_set_entries(key, iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400532 continue;
533 }
Ingo Molnarc5905af2012-02-24 08:31:31 +0100534 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
Jason Barond430d3d2011-03-16 17:29:47 -0400535 if (!jlm)
536 return -ENOMEM;
Jason Baron3821fd32017-02-03 15:42:24 -0500537 if (!static_key_linked(key)) {
538 jlm2 = kzalloc(sizeof(struct static_key_mod),
539 GFP_KERNEL);
540 if (!jlm2) {
541 kfree(jlm);
542 return -ENOMEM;
543 }
544 preempt_disable();
545 jlm2->mod = __module_address((unsigned long)key);
546 preempt_enable();
547 jlm2->entries = static_key_entries(key);
548 jlm2->next = NULL;
549 static_key_set_mod(key, jlm2);
550 static_key_set_linked(key);
551 }
Jason Barond430d3d2011-03-16 17:29:47 -0400552 jlm->mod = mod;
553 jlm->entries = iter;
Jason Baron3821fd32017-02-03 15:42:24 -0500554 jlm->next = static_key_mod(key);
555 static_key_set_mod(key, jlm);
556 static_key_set_linked(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400557
Peter Zijlstra11276d52015-07-24 15:09:55 +0200558 /* Only update if we've changed from our initial state */
559 if (jump_label_type(iter) != jump_label_init_type(iter))
Peter Zijlstra706249c2015-07-24 15:06:37 +0200560 __jump_label_update(key, iter, iter_stop);
Jason Barond430d3d2011-03-16 17:29:47 -0400561 }
562
563 return 0;
564}
565
566static void jump_label_del_module(struct module *mod)
567{
568 struct jump_entry *iter_start = mod->jump_entries;
569 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
570 struct jump_entry *iter;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100571 struct static_key *key = NULL;
572 struct static_key_mod *jlm, **prev;
Jason Barond430d3d2011-03-16 17:29:47 -0400573
574 for (iter = iter_start; iter < iter_stop; iter++) {
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200575 if (jump_entry_key(iter) == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400576 continue;
577
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200578 key = jump_entry_key(iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400579
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930580 if (within_module(iter->key, mod))
Jason Barond430d3d2011-03-16 17:29:47 -0400581 continue;
582
Jason Baron3821fd32017-02-03 15:42:24 -0500583 /* No memory during module load */
584 if (WARN_ON(!static_key_linked(key)))
585 continue;
586
Jason Barond430d3d2011-03-16 17:29:47 -0400587 prev = &key->next;
Jason Baron3821fd32017-02-03 15:42:24 -0500588 jlm = static_key_mod(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400589
590 while (jlm && jlm->mod != mod) {
591 prev = &jlm->next;
592 jlm = jlm->next;
593 }
594
Jason Baron3821fd32017-02-03 15:42:24 -0500595 /* No memory during module load */
596 if (WARN_ON(!jlm))
597 continue;
598
599 if (prev == &key->next)
600 static_key_set_mod(key, jlm->next);
601 else
Jason Barond430d3d2011-03-16 17:29:47 -0400602 *prev = jlm->next;
Jason Baron3821fd32017-02-03 15:42:24 -0500603
604 kfree(jlm);
605
606 jlm = static_key_mod(key);
607 /* if only one etry is left, fold it back into the static_key */
608 if (jlm->next == NULL) {
609 static_key_set_entries(key, jlm->entries);
610 static_key_clear_linked(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400611 kfree(jlm);
612 }
613 }
614}
615
616static void jump_label_invalidate_module_init(struct module *mod)
617{
618 struct jump_entry *iter_start = mod->jump_entries;
619 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
620 struct jump_entry *iter;
621
622 for (iter = iter_start; iter < iter_stop; iter++) {
623 if (within_module_init(iter->code, mod))
624 iter->code = 0;
625 }
626}
627
628static int
629jump_label_module_notify(struct notifier_block *self, unsigned long val,
630 void *data)
631{
632 struct module *mod = data;
633 int ret = 0;
634
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200635 cpus_read_lock();
636 jump_label_lock();
637
Jason Barond430d3d2011-03-16 17:29:47 -0400638 switch (val) {
639 case MODULE_STATE_COMING:
Jason Barond430d3d2011-03-16 17:29:47 -0400640 ret = jump_label_add_module(mod);
Jason Baron3821fd32017-02-03 15:42:24 -0500641 if (ret) {
642 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
Jason Barond430d3d2011-03-16 17:29:47 -0400643 jump_label_del_module(mod);
Jason Baron3821fd32017-02-03 15:42:24 -0500644 }
Jason Barond430d3d2011-03-16 17:29:47 -0400645 break;
646 case MODULE_STATE_GOING:
Jason Barond430d3d2011-03-16 17:29:47 -0400647 jump_label_del_module(mod);
Jason Barond430d3d2011-03-16 17:29:47 -0400648 break;
649 case MODULE_STATE_LIVE:
Jason Barond430d3d2011-03-16 17:29:47 -0400650 jump_label_invalidate_module_init(mod);
Jason Barond430d3d2011-03-16 17:29:47 -0400651 break;
652 }
653
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200654 jump_label_unlock();
655 cpus_read_unlock();
656
Jason Barond430d3d2011-03-16 17:29:47 -0400657 return notifier_from_errno(ret);
658}
659
Wei Yongjun885885f2016-06-17 17:19:40 +0000660static struct notifier_block jump_label_module_nb = {
Jason Barond430d3d2011-03-16 17:29:47 -0400661 .notifier_call = jump_label_module_notify,
662 .priority = 1, /* higher than tracepoints */
663};
664
665static __init int jump_label_init_module(void)
666{
667 return register_module_notifier(&jump_label_module_nb);
668}
669early_initcall(jump_label_init_module);
670
671#endif /* CONFIG_MODULES */
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400672
673/***
674 * jump_label_text_reserved - check if addr range is reserved
675 * @start: start text addr
676 * @end: end text addr
677 *
678 * checks if the text addr located between @start and @end
679 * overlaps with any of the jump label patch addresses. Code
680 * that wants to modify kernel text should first verify that
681 * it does not overlap with any of the jump label addresses.
Jason Baron91bad2f2010-10-01 17:23:48 -0400682 * Caller must hold jump_label_mutex.
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400683 *
684 * returns 1 if there is an overlap, 0 otherwise
685 */
686int jump_label_text_reserved(void *start, void *end)
687{
Jason Barond430d3d2011-03-16 17:29:47 -0400688 int ret = __jump_label_text_reserved(__start___jump_table,
689 __stop___jump_table, start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400690
Jason Barond430d3d2011-03-16 17:29:47 -0400691 if (ret)
692 return ret;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400693
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400694#ifdef CONFIG_MODULES
Jason Barond430d3d2011-03-16 17:29:47 -0400695 ret = __jump_label_mod_text_reserved(start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400696#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -0400697 return ret;
698}
Jason Barond430d3d2011-03-16 17:29:47 -0400699
Peter Zijlstra706249c2015-07-24 15:06:37 +0200700static void jump_label_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400701{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100702 struct jump_entry *stop = __stop___jump_table;
Jason Baron3821fd32017-02-03 15:42:24 -0500703 struct jump_entry *entry;
Jason Baronbf5438fc2010-09-17 11:09:00 -0400704#ifdef CONFIG_MODULES
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930705 struct module *mod;
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800706
Jason Baron3821fd32017-02-03 15:42:24 -0500707 if (static_key_linked(key)) {
708 __jump_label_mod_update(key);
709 return;
710 }
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800711
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930712 preempt_disable();
713 mod = __module_address((unsigned long)key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800714 if (mod)
715 stop = mod->jump_entries + mod->num_jump_entries;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930716 preempt_enable();
Jason Barond430d3d2011-03-16 17:29:47 -0400717#endif
Jason Baron3821fd32017-02-03 15:42:24 -0500718 entry = static_key_entries(key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800719 /* if there are no users, entry can be NULL */
720 if (entry)
Peter Zijlstra706249c2015-07-24 15:06:37 +0200721 __jump_label_update(key, entry, stop);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400722}
723
Peter Zijlstra1987c942015-07-27 18:32:09 +0200724#ifdef CONFIG_STATIC_KEYS_SELFTEST
725static DEFINE_STATIC_KEY_TRUE(sk_true);
726static DEFINE_STATIC_KEY_FALSE(sk_false);
727
728static __init int jump_label_test(void)
729{
730 int i;
731
732 for (i = 0; i < 2; i++) {
733 WARN_ON(static_key_enabled(&sk_true.key) != true);
734 WARN_ON(static_key_enabled(&sk_false.key) != false);
735
736 WARN_ON(!static_branch_likely(&sk_true));
737 WARN_ON(!static_branch_unlikely(&sk_true));
738 WARN_ON(static_branch_likely(&sk_false));
739 WARN_ON(static_branch_unlikely(&sk_false));
740
741 static_branch_disable(&sk_true);
742 static_branch_enable(&sk_false);
743
744 WARN_ON(static_key_enabled(&sk_true.key) == true);
745 WARN_ON(static_key_enabled(&sk_false.key) == false);
746
747 WARN_ON(static_branch_likely(&sk_true));
748 WARN_ON(static_branch_unlikely(&sk_true));
749 WARN_ON(!static_branch_likely(&sk_false));
750 WARN_ON(!static_branch_unlikely(&sk_false));
751
752 static_branch_enable(&sk_true);
753 static_branch_disable(&sk_false);
754 }
755
756 return 0;
757}
758late_initcall(jump_label_test);
759#endif /* STATIC_KEYS_SELFTEST */
760
761#endif /* HAVE_JUMP_LABEL */