blob: d11c506a6ac3db63e9475ee45d3c4bd34aa47a73 [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
82void 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}
91EXPORT_SYMBOL_GPL(static_key_enable);
92
93void 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}
102EXPORT_SYMBOL_GPL(static_key_disable);
103
Ingo Molnarc5905af2012-02-24 08:31:31 +0100104void static_key_slow_inc(struct static_key *key)
Jason Baronbf5438fc2010-09-17 11:09:00 -0400105{
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200106 int v, v1;
107
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200108 STATIC_KEY_CHECK_USE();
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200109
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 Baronbf5438fc2010-09-17 11:09:00 -0400127
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200128 cpus_read_lock();
Jason Baron91bad2f2010-10-01 17:23:48 -0400129 jump_label_lock();
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200130 if (atomic_read(&key->enabled) == 0) {
131 atomic_set(&key->enabled, -1);
Peter Zijlstra706249c2015-07-24 15:06:37 +0200132 jump_label_update(key);
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200133 atomic_set(&key->enabled, 1);
134 } else {
135 atomic_inc(&key->enabled);
136 }
Jason Barond430d3d2011-03-16 17:29:47 -0400137 jump_label_unlock();
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200138 cpus_read_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400139}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100140EXPORT_SYMBOL_GPL(static_key_slow_inc);
Jason Barond430d3d2011-03-16 17:29:47 -0400141
Ingo Molnarc5905af2012-02-24 08:31:31 +0100142static void __static_key_slow_dec(struct static_key *key,
Gleb Natapovb2029522011-11-27 17:59:09 +0200143 unsigned long rate_limit, struct delayed_work *work)
Jason Barond430d3d2011-03-16 17:29:47 -0400144{
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200145 cpus_read_lock();
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200146 /*
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 Baronfadf0462012-02-21 15:02:53 -0500153 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 Gleixnerf2545b2d2017-05-24 10:15:35 +0200156 cpus_read_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400157 return;
Jason Baronfadf0462012-02-21 15:02:53 -0500158 }
Jason Barond430d3d2011-03-16 17:29:47 -0400159
Gleb Natapovb2029522011-11-27 17:59:09 +0200160 if (rate_limit) {
161 atomic_inc(&key->enabled);
162 schedule_delayed_work(work, rate_limit);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100163 } else {
Peter Zijlstra706249c2015-07-24 15:06:37 +0200164 jump_label_update(key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100165 }
Jason Baron91bad2f2010-10-01 17:23:48 -0400166 jump_label_unlock();
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200167 cpus_read_unlock();
Jason Baronbf5438fc2010-09-17 11:09:00 -0400168}
169
Gleb Natapovb2029522011-11-27 17:59:09 +0200170static void jump_label_update_timeout(struct work_struct *work)
171{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100172 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 Natapovb2029522011-11-27 17:59:09 +0200175}
176
Ingo Molnarc5905af2012-02-24 08:31:31 +0100177void static_key_slow_dec(struct static_key *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200178{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200179 STATIC_KEY_CHECK_USE();
Ingo Molnarc5905af2012-02-24 08:31:31 +0100180 __static_key_slow_dec(key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200181}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100182EXPORT_SYMBOL_GPL(static_key_slow_dec);
Gleb Natapovb2029522011-11-27 17:59:09 +0200183
Ingo Molnarc5905af2012-02-24 08:31:31 +0100184void static_key_slow_dec_deferred(struct static_key_deferred *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200185{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200186 STATIC_KEY_CHECK_USE();
Ingo Molnarc5905af2012-02-24 08:31:31 +0100187 __static_key_slow_dec(&key->key, key->timeout, &key->work);
Gleb Natapovb2029522011-11-27 17:59:09 +0200188}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100189EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
Gleb Natapovb2029522011-11-27 17:59:09 +0200190
David Matlackb6416e62016-12-16 14:30:35 -0800191void static_key_deferred_flush(struct static_key_deferred *key)
192{
193 STATIC_KEY_CHECK_USE();
194 flush_delayed_work(&key->work);
195}
196EXPORT_SYMBOL_GPL(static_key_deferred_flush);
197
Ingo Molnarc5905af2012-02-24 08:31:31 +0100198void jump_label_rate_limit(struct static_key_deferred *key,
Gleb Natapovb2029522011-11-27 17:59:09 +0200199 unsigned long rl)
200{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200201 STATIC_KEY_CHECK_USE();
Gleb Natapovb2029522011-11-27 17:59:09 +0200202 key->timeout = rl;
203 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
204}
Gleb Natapova181dc12012-08-05 15:58:29 +0300205EXPORT_SYMBOL_GPL(jump_label_rate_limit);
Gleb Natapovb2029522011-11-27 17:59:09 +0200206
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400207static 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 Barond430d3d2011-03-16 17:29:47 -0400216static int __jump_label_text_reserved(struct jump_entry *iter_start,
217 struct jump_entry *iter_stop, void *start, void *end)
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400218{
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400219 struct jump_entry *iter;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400220
Jason Barond430d3d2011-03-16 17:29:47 -0400221 iter = iter_start;
222 while (iter < iter_stop) {
223 if (addr_conflict(iter, start, end))
224 return 1;
225 iter++;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400226 }
Jason Barond430d3d2011-03-16 17:29:47 -0400227
228 return 0;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400229}
230
Peter Zijlstra706249c2015-07-24 15:06:37 +0200231/*
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700232 * 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 Zijlstra9cdbe1c2011-12-06 17:27:29 +0100237void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700238 enum jump_label_type type)
239{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200240 arch_jump_label_transform(entry, type);
Jason Barond430d3d2011-03-16 17:29:47 -0400241}
242
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200243static inline struct jump_entry *static_key_entries(struct static_key *key)
244{
Jason Baron3821fd32017-02-03 15:42:24 -0500245 WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
246 return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200247}
248
Peter Zijlstra706249c2015-07-24 15:06:37 +0200249static inline bool static_key_type(struct static_key *key)
250{
Jason Baron3821fd32017-02-03 15:42:24 -0500251 return key->type & JUMP_TYPE_TRUE;
252}
253
254static inline bool static_key_linked(struct static_key *key)
255{
256 return key->type & JUMP_TYPE_LINKED;
257}
258
259static inline void static_key_clear_linked(struct static_key *key)
260{
261 key->type &= ~JUMP_TYPE_LINKED;
262}
263
264static inline void static_key_set_linked(struct static_key *key)
265{
266 key->type |= JUMP_TYPE_LINKED;
Peter Zijlstra706249c2015-07-24 15:06:37 +0200267}
268
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200269static inline struct static_key *jump_entry_key(struct jump_entry *entry)
270{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200271 return (struct static_key *)((unsigned long)entry->key & ~1UL);
272}
273
274static bool jump_entry_branch(struct jump_entry *entry)
275{
276 return (unsigned long)entry->key & 1UL;
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200277}
278
Jason Baron3821fd32017-02-03 15:42:24 -0500279/***
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 */
288static 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 Zijlstra706249c2015-07-24 15:06:37 +0200299static enum jump_label_type jump_label_type(struct jump_entry *entry)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100300{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200301 struct static_key *key = jump_entry_key(entry);
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200302 bool enabled = static_key_enabled(key);
Peter Zijlstra11276d52015-07-24 15:09:55 +0200303 bool branch = jump_entry_branch(entry);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100304
Peter Zijlstra11276d52015-07-24 15:09:55 +0200305 /* See the comment in linux/jump_label.h */
306 return enabled ^ branch;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100307}
308
Peter Zijlstra706249c2015-07-24 15:06:37 +0200309static 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 Fitzhardinge97ce2c82011-10-12 16:17:54 -0700324void __init jump_label_init(void)
Jason Barond430d3d2011-03-16 17:29:47 -0400325{
326 struct jump_entry *iter_start = __start___jump_table;
327 struct jump_entry *iter_stop = __stop___jump_table;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100328 struct static_key *key = NULL;
Jason Barond430d3d2011-03-16 17:29:47 -0400329 struct jump_entry *iter;
330
Jason Baron1f69bf92016-08-03 13:46:36 -0700331 /*
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 Haoe3f91082016-07-23 14:42:37 +0530340 if (static_key_initialized)
341 return;
342
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200343 cpus_read_lock();
Jason Barond430d3d2011-03-16 17:29:47 -0400344 jump_label_lock();
345 jump_label_sort_entries(iter_start, iter_stop);
346
347 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100348 struct static_key *iterk;
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700349
Peter Zijlstra11276d52015-07-24 15:09:55 +0200350 /* rewrite NOPs */
351 if (jump_label_type(iter) == JUMP_LABEL_NOP)
352 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
353
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200354 iterk = jump_entry_key(iter);
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700355 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400356 continue;
357
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700358 key = iterk;
Jason Baron3821fd32017-02-03 15:42:24 -0500359 static_key_set_entries(key, iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400360 }
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200361 static_key_initialized = true;
Jason Barond430d3d2011-03-16 17:29:47 -0400362 jump_label_unlock();
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200363 cpus_read_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400364}
Jason Barond430d3d2011-03-16 17:29:47 -0400365
366#ifdef CONFIG_MODULES
367
Peter Zijlstra11276d52015-07-24 15:09:55 +0200368static 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 Molnarc5905af2012-02-24 08:31:31 +0100378struct static_key_mod {
379 struct static_key_mod *next;
Jason Barond430d3d2011-03-16 17:29:47 -0400380 struct jump_entry *entries;
381 struct module *mod;
382};
383
Jason Baron3821fd32017-02-03 15:42:24 -0500384static 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 */
396static 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 Barond430d3d2011-03-16 17:29:47 -0400407static int __jump_label_mod_text_reserved(void *start, void *end)
408{
409 struct module *mod;
410
Rusty Russellbdc9f372016-07-27 12:17:35 +0930411 preempt_disable();
Jason Barond430d3d2011-03-16 17:29:47 -0400412 mod = __module_text_address((unsigned long)start);
Rusty Russellbdc9f372016-07-27 12:17:35 +0930413 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
414 preempt_enable();
415
Jason Barond430d3d2011-03-16 17:29:47 -0400416 if (!mod)
417 return 0;
418
Jason Barond430d3d2011-03-16 17:29:47 -0400419
420 return __jump_label_text_reserved(mod->jump_entries,
421 mod->jump_entries + mod->num_jump_entries,
422 start, end);
423}
424
Peter Zijlstra706249c2015-07-24 15:06:37 +0200425static void __jump_label_mod_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400426{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200427 struct static_key_mod *mod;
Jason Barond430d3d2011-03-16 17:29:47 -0400428
Jason Baron3821fd32017-02-03 15:42:24 -0500429 for (mod = static_key_mod(key); mod; mod = mod->next) {
430 struct jump_entry *stop;
431 struct module *m;
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200432
Jason Baron3821fd32017-02-03 15:42:24 -0500433 /*
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 Barond430d3d2011-03-16 17:29:47 -0400446 }
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 */
457void 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 Zijlstra11276d52015-07-24 15:09:55 +0200467 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 Barond430d3d2011-03-16 17:29:47 -0400472}
473
474static 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 Molnarc5905af2012-02-24 08:31:31 +0100479 struct static_key *key = NULL;
Jason Baron3821fd32017-02-03 15:42:24 -0500480 struct static_key_mod *jlm, *jlm2;
Jason Barond430d3d2011-03-16 17:29:47 -0400481
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 Molnarc5905af2012-02-24 08:31:31 +0100489 struct static_key *iterk;
490
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200491 iterk = jump_entry_key(iter);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100492 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400493 continue;
494
Ingo Molnarc5905af2012-02-24 08:31:31 +0100495 key = iterk;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930496 if (within_module(iter->key, mod)) {
Jason Baron3821fd32017-02-03 15:42:24 -0500497 static_key_set_entries(key, iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400498 continue;
499 }
Ingo Molnarc5905af2012-02-24 08:31:31 +0100500 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
Jason Barond430d3d2011-03-16 17:29:47 -0400501 if (!jlm)
502 return -ENOMEM;
Jason Baron3821fd32017-02-03 15:42:24 -0500503 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 Barond430d3d2011-03-16 17:29:47 -0400518 jlm->mod = mod;
519 jlm->entries = iter;
Jason Baron3821fd32017-02-03 15:42:24 -0500520 jlm->next = static_key_mod(key);
521 static_key_set_mod(key, jlm);
522 static_key_set_linked(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400523
Peter Zijlstra11276d52015-07-24 15:09:55 +0200524 /* Only update if we've changed from our initial state */
525 if (jump_label_type(iter) != jump_label_init_type(iter))
Peter Zijlstra706249c2015-07-24 15:06:37 +0200526 __jump_label_update(key, iter, iter_stop);
Jason Barond430d3d2011-03-16 17:29:47 -0400527 }
528
529 return 0;
530}
531
532static 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 Molnarc5905af2012-02-24 08:31:31 +0100537 struct static_key *key = NULL;
538 struct static_key_mod *jlm, **prev;
Jason Barond430d3d2011-03-16 17:29:47 -0400539
540 for (iter = iter_start; iter < iter_stop; iter++) {
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200541 if (jump_entry_key(iter) == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400542 continue;
543
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200544 key = jump_entry_key(iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400545
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930546 if (within_module(iter->key, mod))
Jason Barond430d3d2011-03-16 17:29:47 -0400547 continue;
548
Jason Baron3821fd32017-02-03 15:42:24 -0500549 /* No memory during module load */
550 if (WARN_ON(!static_key_linked(key)))
551 continue;
552
Jason Barond430d3d2011-03-16 17:29:47 -0400553 prev = &key->next;
Jason Baron3821fd32017-02-03 15:42:24 -0500554 jlm = static_key_mod(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400555
556 while (jlm && jlm->mod != mod) {
557 prev = &jlm->next;
558 jlm = jlm->next;
559 }
560
Jason Baron3821fd32017-02-03 15:42:24 -0500561 /* 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 Barond430d3d2011-03-16 17:29:47 -0400568 *prev = jlm->next;
Jason Baron3821fd32017-02-03 15:42:24 -0500569
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 Barond430d3d2011-03-16 17:29:47 -0400577 kfree(jlm);
578 }
579 }
580}
581
582static 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
594static int
595jump_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 Gleixnerf2545b2d2017-05-24 10:15:35 +0200601 cpus_read_lock();
602 jump_label_lock();
603
Jason Barond430d3d2011-03-16 17:29:47 -0400604 switch (val) {
605 case MODULE_STATE_COMING:
Jason Barond430d3d2011-03-16 17:29:47 -0400606 ret = jump_label_add_module(mod);
Jason Baron3821fd32017-02-03 15:42:24 -0500607 if (ret) {
608 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
Jason Barond430d3d2011-03-16 17:29:47 -0400609 jump_label_del_module(mod);
Jason Baron3821fd32017-02-03 15:42:24 -0500610 }
Jason Barond430d3d2011-03-16 17:29:47 -0400611 break;
612 case MODULE_STATE_GOING:
Jason Barond430d3d2011-03-16 17:29:47 -0400613 jump_label_del_module(mod);
Jason Barond430d3d2011-03-16 17:29:47 -0400614 break;
615 case MODULE_STATE_LIVE:
Jason Barond430d3d2011-03-16 17:29:47 -0400616 jump_label_invalidate_module_init(mod);
Jason Barond430d3d2011-03-16 17:29:47 -0400617 break;
618 }
619
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200620 jump_label_unlock();
621 cpus_read_unlock();
622
Jason Barond430d3d2011-03-16 17:29:47 -0400623 return notifier_from_errno(ret);
624}
625
Wei Yongjun885885f2016-06-17 17:19:40 +0000626static struct notifier_block jump_label_module_nb = {
Jason Barond430d3d2011-03-16 17:29:47 -0400627 .notifier_call = jump_label_module_notify,
628 .priority = 1, /* higher than tracepoints */
629};
630
631static __init int jump_label_init_module(void)
632{
633 return register_module_notifier(&jump_label_module_nb);
634}
635early_initcall(jump_label_init_module);
636
637#endif /* CONFIG_MODULES */
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400638
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 Baron91bad2f2010-10-01 17:23:48 -0400648 * Caller must hold jump_label_mutex.
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400649 *
650 * returns 1 if there is an overlap, 0 otherwise
651 */
652int jump_label_text_reserved(void *start, void *end)
653{
Jason Barond430d3d2011-03-16 17:29:47 -0400654 int ret = __jump_label_text_reserved(__start___jump_table,
655 __stop___jump_table, start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400656
Jason Barond430d3d2011-03-16 17:29:47 -0400657 if (ret)
658 return ret;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400659
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400660#ifdef CONFIG_MODULES
Jason Barond430d3d2011-03-16 17:29:47 -0400661 ret = __jump_label_mod_text_reserved(start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400662#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -0400663 return ret;
664}
Jason Barond430d3d2011-03-16 17:29:47 -0400665
Peter Zijlstra706249c2015-07-24 15:06:37 +0200666static void jump_label_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400667{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100668 struct jump_entry *stop = __stop___jump_table;
Jason Baron3821fd32017-02-03 15:42:24 -0500669 struct jump_entry *entry;
Jason Baronbf5438fc2010-09-17 11:09:00 -0400670#ifdef CONFIG_MODULES
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930671 struct module *mod;
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800672
Jason Baron3821fd32017-02-03 15:42:24 -0500673 if (static_key_linked(key)) {
674 __jump_label_mod_update(key);
675 return;
676 }
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800677
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930678 preempt_disable();
679 mod = __module_address((unsigned long)key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800680 if (mod)
681 stop = mod->jump_entries + mod->num_jump_entries;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930682 preempt_enable();
Jason Barond430d3d2011-03-16 17:29:47 -0400683#endif
Jason Baron3821fd32017-02-03 15:42:24 -0500684 entry = static_key_entries(key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800685 /* if there are no users, entry can be NULL */
686 if (entry)
Peter Zijlstra706249c2015-07-24 15:06:37 +0200687 __jump_label_update(key, entry, stop);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400688}
689
Peter Zijlstra1987c942015-07-27 18:32:09 +0200690#ifdef CONFIG_STATIC_KEYS_SELFTEST
691static DEFINE_STATIC_KEY_TRUE(sk_true);
692static DEFINE_STATIC_KEY_FALSE(sk_false);
693
694static __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}
724late_initcall(jump_label_test);
725#endif /* STATIC_KEYS_SELFTEST */
726
727#endif /* HAVE_JUMP_LABEL */