blob: e7214093dcd143e61325956064c5f84d772aa110 [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
Peter Zijlstrace48c1462018-01-22 22:53:28 +010082void 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
Borislav Petkov5cdda512017-10-18 17:24:28 +020086 STATIC_KEY_CHECK_USE(key);
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
Marc Zyngier5a405272017-08-01 09:02:56 +0100129void static_key_enable_cpuslocked(struct static_key *key)
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200130{
Borislav Petkov5cdda512017-10-18 17:24:28 +0200131 STATIC_KEY_CHECK_USE(key);
Marc Zyngier5a405272017-08-01 09:02:56 +0100132
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200133 if (atomic_read(&key->enabled) > 0) {
134 WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
135 return;
136 }
137
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200138 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();
Marc Zyngier5a405272017-08-01 09:02:56 +0100148}
149EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
150
151void static_key_enable(struct static_key *key)
152{
153 cpus_read_lock();
154 static_key_enable_cpuslocked(key);
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200155 cpus_read_unlock();
156}
157EXPORT_SYMBOL_GPL(static_key_enable);
158
Marc Zyngier5a405272017-08-01 09:02:56 +0100159void static_key_disable_cpuslocked(struct static_key *key)
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200160{
Borislav Petkov5cdda512017-10-18 17:24:28 +0200161 STATIC_KEY_CHECK_USE(key);
Marc Zyngier5a405272017-08-01 09:02:56 +0100162
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200163 if (atomic_read(&key->enabled) != 1) {
164 WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
165 return;
166 }
167
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200168 jump_label_lock();
169 if (atomic_cmpxchg(&key->enabled, 1, 0))
170 jump_label_update(key);
171 jump_label_unlock();
Marc Zyngier5a405272017-08-01 09:02:56 +0100172}
173EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
174
175void static_key_disable(struct static_key *key)
176{
177 cpus_read_lock();
178 static_key_disable_cpuslocked(key);
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200179 cpus_read_unlock();
180}
181EXPORT_SYMBOL_GPL(static_key_disable);
182
Peter Zijlstrace48c1462018-01-22 22:53:28 +0100183static void __static_key_slow_dec_cpuslocked(struct static_key *key,
Marc Zyngier8b7b4122017-08-01 09:02:55 +0100184 unsigned long rate_limit,
185 struct delayed_work *work)
Jason Barond430d3d2011-03-16 17:29:47 -0400186{
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200187 /*
188 * The negative count check is valid even when a negative
189 * key->enabled is in use by static_key_slow_inc(); a
190 * __static_key_slow_dec() before the first static_key_slow_inc()
191 * returns is unbalanced, because all other static_key_slow_inc()
192 * instances block while the update is in progress.
193 */
Jason Baronfadf0462012-02-21 15:02:53 -0500194 if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
195 WARN(atomic_read(&key->enabled) < 0,
196 "jump label: negative count!\n");
Jason Barond430d3d2011-03-16 17:29:47 -0400197 return;
Jason Baronfadf0462012-02-21 15:02:53 -0500198 }
Jason Barond430d3d2011-03-16 17:29:47 -0400199
Gleb Natapovb2029522011-11-27 17:59:09 +0200200 if (rate_limit) {
201 atomic_inc(&key->enabled);
202 schedule_delayed_work(work, rate_limit);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100203 } else {
Peter Zijlstra706249c2015-07-24 15:06:37 +0200204 jump_label_update(key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100205 }
Jason Baron91bad2f2010-10-01 17:23:48 -0400206 jump_label_unlock();
Marc Zyngier8b7b4122017-08-01 09:02:55 +0100207}
208
209static void __static_key_slow_dec(struct static_key *key,
210 unsigned long rate_limit,
211 struct delayed_work *work)
212{
213 cpus_read_lock();
Peter Zijlstrace48c1462018-01-22 22:53:28 +0100214 __static_key_slow_dec_cpuslocked(key, rate_limit, work);
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200215 cpus_read_unlock();
Jason Baronbf5438fc2010-09-17 11:09:00 -0400216}
217
Gleb Natapovb2029522011-11-27 17:59:09 +0200218static void jump_label_update_timeout(struct work_struct *work)
219{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100220 struct static_key_deferred *key =
221 container_of(work, struct static_key_deferred, work.work);
222 __static_key_slow_dec(&key->key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200223}
224
Ingo Molnarc5905af2012-02-24 08:31:31 +0100225void static_key_slow_dec(struct static_key *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200226{
Borislav Petkov5cdda512017-10-18 17:24:28 +0200227 STATIC_KEY_CHECK_USE(key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100228 __static_key_slow_dec(key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200229}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100230EXPORT_SYMBOL_GPL(static_key_slow_dec);
Gleb Natapovb2029522011-11-27 17:59:09 +0200231
Peter Zijlstrace48c1462018-01-22 22:53:28 +0100232void static_key_slow_dec_cpuslocked(struct static_key *key)
233{
234 STATIC_KEY_CHECK_USE(key);
235 __static_key_slow_dec_cpuslocked(key, 0, NULL);
236}
237
Ingo Molnarc5905af2012-02-24 08:31:31 +0100238void static_key_slow_dec_deferred(struct static_key_deferred *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200239{
Borislav Petkov5cdda512017-10-18 17:24:28 +0200240 STATIC_KEY_CHECK_USE(key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100241 __static_key_slow_dec(&key->key, key->timeout, &key->work);
Gleb Natapovb2029522011-11-27 17:59:09 +0200242}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100243EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
Gleb Natapovb2029522011-11-27 17:59:09 +0200244
David Matlackb6416e62016-12-16 14:30:35 -0800245void static_key_deferred_flush(struct static_key_deferred *key)
246{
Borislav Petkov5cdda512017-10-18 17:24:28 +0200247 STATIC_KEY_CHECK_USE(key);
David Matlackb6416e62016-12-16 14:30:35 -0800248 flush_delayed_work(&key->work);
249}
250EXPORT_SYMBOL_GPL(static_key_deferred_flush);
251
Ingo Molnarc5905af2012-02-24 08:31:31 +0100252void jump_label_rate_limit(struct static_key_deferred *key,
Gleb Natapovb2029522011-11-27 17:59:09 +0200253 unsigned long rl)
254{
Borislav Petkov5cdda512017-10-18 17:24:28 +0200255 STATIC_KEY_CHECK_USE(key);
Gleb Natapovb2029522011-11-27 17:59:09 +0200256 key->timeout = rl;
257 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
258}
Gleb Natapova181dc12012-08-05 15:58:29 +0300259EXPORT_SYMBOL_GPL(jump_label_rate_limit);
Gleb Natapovb2029522011-11-27 17:59:09 +0200260
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400261static int addr_conflict(struct jump_entry *entry, void *start, void *end)
262{
263 if (entry->code <= (unsigned long)end &&
264 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
265 return 1;
266
267 return 0;
268}
269
Jason Barond430d3d2011-03-16 17:29:47 -0400270static int __jump_label_text_reserved(struct jump_entry *iter_start,
271 struct jump_entry *iter_stop, void *start, void *end)
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400272{
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400273 struct jump_entry *iter;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400274
Jason Barond430d3d2011-03-16 17:29:47 -0400275 iter = iter_start;
276 while (iter < iter_stop) {
277 if (addr_conflict(iter, start, end))
278 return 1;
279 iter++;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400280 }
Jason Barond430d3d2011-03-16 17:29:47 -0400281
282 return 0;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400283}
284
Peter Zijlstra706249c2015-07-24 15:06:37 +0200285/*
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700286 * Update code which is definitely not currently executing.
287 * Architectures which need heavyweight synchronization to modify
288 * running code can override this to make the non-live update case
289 * cheaper.
290 */
Peter Zijlstra9cdbe1c2011-12-06 17:27:29 +0100291void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700292 enum jump_label_type type)
293{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200294 arch_jump_label_transform(entry, type);
Jason Barond430d3d2011-03-16 17:29:47 -0400295}
296
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200297static inline struct jump_entry *static_key_entries(struct static_key *key)
298{
Jason Baron3821fd32017-02-03 15:42:24 -0500299 WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
300 return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200301}
302
Peter Zijlstra706249c2015-07-24 15:06:37 +0200303static inline bool static_key_type(struct static_key *key)
304{
Jason Baron3821fd32017-02-03 15:42:24 -0500305 return key->type & JUMP_TYPE_TRUE;
306}
307
308static inline bool static_key_linked(struct static_key *key)
309{
310 return key->type & JUMP_TYPE_LINKED;
311}
312
313static inline void static_key_clear_linked(struct static_key *key)
314{
315 key->type &= ~JUMP_TYPE_LINKED;
316}
317
318static inline void static_key_set_linked(struct static_key *key)
319{
320 key->type |= JUMP_TYPE_LINKED;
Peter Zijlstra706249c2015-07-24 15:06:37 +0200321}
322
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200323static inline struct static_key *jump_entry_key(struct jump_entry *entry)
324{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200325 return (struct static_key *)((unsigned long)entry->key & ~1UL);
326}
327
328static bool jump_entry_branch(struct jump_entry *entry)
329{
330 return (unsigned long)entry->key & 1UL;
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200331}
332
Jason Baron3821fd32017-02-03 15:42:24 -0500333/***
334 * A 'struct static_key' uses a union such that it either points directly
335 * to a table of 'struct jump_entry' or to a linked list of modules which in
336 * turn point to 'struct jump_entry' tables.
337 *
338 * The two lower bits of the pointer are used to keep track of which pointer
339 * type is in use and to store the initial branch direction, we use an access
340 * function which preserves these bits.
341 */
342static void static_key_set_entries(struct static_key *key,
343 struct jump_entry *entries)
344{
345 unsigned long type;
346
347 WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
348 type = key->type & JUMP_TYPE_MASK;
349 key->entries = entries;
350 key->type |= type;
351}
352
Peter Zijlstra706249c2015-07-24 15:06:37 +0200353static enum jump_label_type jump_label_type(struct jump_entry *entry)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100354{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200355 struct static_key *key = jump_entry_key(entry);
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200356 bool enabled = static_key_enabled(key);
Peter Zijlstra11276d52015-07-24 15:09:55 +0200357 bool branch = jump_entry_branch(entry);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100358
Peter Zijlstra11276d52015-07-24 15:09:55 +0200359 /* See the comment in linux/jump_label.h */
360 return enabled ^ branch;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100361}
362
Peter Zijlstra706249c2015-07-24 15:06:37 +0200363static void __jump_label_update(struct static_key *key,
364 struct jump_entry *entry,
365 struct jump_entry *stop)
366{
367 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
368 /*
Josh Poimboeufdc1dd182018-02-20 11:37:52 -0600369 * An entry->code of 0 indicates an entry which has been
370 * disabled because it was in an init text area.
Peter Zijlstra706249c2015-07-24 15:06:37 +0200371 */
Josh Poimboeufdc1dd182018-02-20 11:37:52 -0600372 if (entry->code) {
373 if (kernel_text_address(entry->code))
374 arch_jump_label_transform(entry, jump_label_type(entry));
375 else
Josh Poimboeufaf1d8302018-03-14 10:24:20 -0500376 WARN_ONCE(1, "can't patch jump_label at %pS",
377 (void *)(unsigned long)entry->code);
Josh Poimboeufdc1dd182018-02-20 11:37:52 -0600378 }
Peter Zijlstra706249c2015-07-24 15:06:37 +0200379 }
380}
381
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700382void __init jump_label_init(void)
Jason Barond430d3d2011-03-16 17:29:47 -0400383{
384 struct jump_entry *iter_start = __start___jump_table;
385 struct jump_entry *iter_stop = __stop___jump_table;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100386 struct static_key *key = NULL;
Jason Barond430d3d2011-03-16 17:29:47 -0400387 struct jump_entry *iter;
388
Jason Baron1f69bf92016-08-03 13:46:36 -0700389 /*
390 * Since we are initializing the static_key.enabled field with
391 * with the 'raw' int values (to avoid pulling in atomic.h) in
392 * jump_label.h, let's make sure that is safe. There are only two
393 * cases to check since we initialize to 0 or 1.
394 */
395 BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
396 BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
397
Kevin Haoe3f91082016-07-23 14:42:37 +0530398 if (static_key_initialized)
399 return;
400
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200401 cpus_read_lock();
Jason Barond430d3d2011-03-16 17:29:47 -0400402 jump_label_lock();
403 jump_label_sort_entries(iter_start, iter_stop);
404
405 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100406 struct static_key *iterk;
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700407
Peter Zijlstra11276d52015-07-24 15:09:55 +0200408 /* rewrite NOPs */
409 if (jump_label_type(iter) == JUMP_LABEL_NOP)
410 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
411
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200412 iterk = jump_entry_key(iter);
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700413 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400414 continue;
415
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700416 key = iterk;
Jason Baron3821fd32017-02-03 15:42:24 -0500417 static_key_set_entries(key, iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400418 }
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200419 static_key_initialized = true;
Jason Barond430d3d2011-03-16 17:29:47 -0400420 jump_label_unlock();
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200421 cpus_read_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400422}
Jason Barond430d3d2011-03-16 17:29:47 -0400423
Josh Poimboeuf33352242018-02-20 11:37:51 -0600424/* Disable any jump label entries in __init code */
425void __init jump_label_invalidate_init(void)
426{
427 struct jump_entry *iter_start = __start___jump_table;
428 struct jump_entry *iter_stop = __stop___jump_table;
429 struct jump_entry *iter;
430
431 for (iter = iter_start; iter < iter_stop; iter++) {
Josh Poimboeuf9fbcc572018-02-20 11:37:53 -0600432 if (init_kernel_text(iter->code))
Josh Poimboeuf33352242018-02-20 11:37:51 -0600433 iter->code = 0;
434 }
435}
436
Jason Barond430d3d2011-03-16 17:29:47 -0400437#ifdef CONFIG_MODULES
438
Peter Zijlstra11276d52015-07-24 15:09:55 +0200439static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
440{
441 struct static_key *key = jump_entry_key(entry);
442 bool type = static_key_type(key);
443 bool branch = jump_entry_branch(entry);
444
445 /* See the comment in linux/jump_label.h */
446 return type ^ branch;
447}
448
Ingo Molnarc5905af2012-02-24 08:31:31 +0100449struct static_key_mod {
450 struct static_key_mod *next;
Jason Barond430d3d2011-03-16 17:29:47 -0400451 struct jump_entry *entries;
452 struct module *mod;
453};
454
Jason Baron3821fd32017-02-03 15:42:24 -0500455static inline struct static_key_mod *static_key_mod(struct static_key *key)
456{
457 WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
458 return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
459}
460
461/***
462 * key->type and key->next are the same via union.
463 * This sets key->next and preserves the type bits.
464 *
465 * See additional comments above static_key_set_entries().
466 */
467static void static_key_set_mod(struct static_key *key,
468 struct static_key_mod *mod)
469{
470 unsigned long type;
471
472 WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
473 type = key->type & JUMP_TYPE_MASK;
474 key->next = mod;
475 key->type |= type;
476}
477
Jason Barond430d3d2011-03-16 17:29:47 -0400478static int __jump_label_mod_text_reserved(void *start, void *end)
479{
480 struct module *mod;
481
Rusty Russellbdc9f372016-07-27 12:17:35 +0930482 preempt_disable();
Jason Barond430d3d2011-03-16 17:29:47 -0400483 mod = __module_text_address((unsigned long)start);
Rusty Russellbdc9f372016-07-27 12:17:35 +0930484 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
485 preempt_enable();
486
Jason Barond430d3d2011-03-16 17:29:47 -0400487 if (!mod)
488 return 0;
489
Jason Barond430d3d2011-03-16 17:29:47 -0400490
491 return __jump_label_text_reserved(mod->jump_entries,
492 mod->jump_entries + mod->num_jump_entries,
493 start, end);
494}
495
Peter Zijlstra706249c2015-07-24 15:06:37 +0200496static void __jump_label_mod_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400497{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200498 struct static_key_mod *mod;
Jason Barond430d3d2011-03-16 17:29:47 -0400499
Jason Baron3821fd32017-02-03 15:42:24 -0500500 for (mod = static_key_mod(key); mod; mod = mod->next) {
501 struct jump_entry *stop;
502 struct module *m;
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200503
Jason Baron3821fd32017-02-03 15:42:24 -0500504 /*
505 * NULL if the static_key is defined in a module
506 * that does not use it
507 */
508 if (!mod->entries)
509 continue;
510
511 m = mod->mod;
512 if (!m)
513 stop = __stop___jump_table;
514 else
515 stop = m->jump_entries + m->num_jump_entries;
516 __jump_label_update(key, mod->entries, stop);
Jason Barond430d3d2011-03-16 17:29:47 -0400517 }
518}
519
520/***
521 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
522 * @mod: module to patch
523 *
524 * Allow for run-time selection of the optimal nops. Before the module
525 * loads patch these with arch_get_jump_label_nop(), which is specified by
526 * the arch specific jump label code.
527 */
528void jump_label_apply_nops(struct module *mod)
529{
530 struct jump_entry *iter_start = mod->jump_entries;
531 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
532 struct jump_entry *iter;
533
534 /* if the module doesn't have jump label entries, just return */
535 if (iter_start == iter_stop)
536 return;
537
Peter Zijlstra11276d52015-07-24 15:09:55 +0200538 for (iter = iter_start; iter < iter_stop; iter++) {
539 /* Only write NOPs for arch_branch_static(). */
540 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
541 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
542 }
Jason Barond430d3d2011-03-16 17:29:47 -0400543}
544
545static int jump_label_add_module(struct module *mod)
546{
547 struct jump_entry *iter_start = mod->jump_entries;
548 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
549 struct jump_entry *iter;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100550 struct static_key *key = NULL;
Jason Baron3821fd32017-02-03 15:42:24 -0500551 struct static_key_mod *jlm, *jlm2;
Jason Barond430d3d2011-03-16 17:29:47 -0400552
553 /* if the module doesn't have jump label entries, just return */
554 if (iter_start == iter_stop)
555 return 0;
556
557 jump_label_sort_entries(iter_start, iter_stop);
558
559 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100560 struct static_key *iterk;
561
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200562 iterk = jump_entry_key(iter);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100563 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400564 continue;
565
Ingo Molnarc5905af2012-02-24 08:31:31 +0100566 key = iterk;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930567 if (within_module(iter->key, mod)) {
Jason Baron3821fd32017-02-03 15:42:24 -0500568 static_key_set_entries(key, iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400569 continue;
570 }
Ingo Molnarc5905af2012-02-24 08:31:31 +0100571 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
Jason Barond430d3d2011-03-16 17:29:47 -0400572 if (!jlm)
573 return -ENOMEM;
Jason Baron3821fd32017-02-03 15:42:24 -0500574 if (!static_key_linked(key)) {
575 jlm2 = kzalloc(sizeof(struct static_key_mod),
576 GFP_KERNEL);
577 if (!jlm2) {
578 kfree(jlm);
579 return -ENOMEM;
580 }
581 preempt_disable();
582 jlm2->mod = __module_address((unsigned long)key);
583 preempt_enable();
584 jlm2->entries = static_key_entries(key);
585 jlm2->next = NULL;
586 static_key_set_mod(key, jlm2);
587 static_key_set_linked(key);
588 }
Jason Barond430d3d2011-03-16 17:29:47 -0400589 jlm->mod = mod;
590 jlm->entries = iter;
Jason Baron3821fd32017-02-03 15:42:24 -0500591 jlm->next = static_key_mod(key);
592 static_key_set_mod(key, jlm);
593 static_key_set_linked(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400594
Peter Zijlstra11276d52015-07-24 15:09:55 +0200595 /* Only update if we've changed from our initial state */
596 if (jump_label_type(iter) != jump_label_init_type(iter))
Peter Zijlstra706249c2015-07-24 15:06:37 +0200597 __jump_label_update(key, iter, iter_stop);
Jason Barond430d3d2011-03-16 17:29:47 -0400598 }
599
600 return 0;
601}
602
603static void jump_label_del_module(struct module *mod)
604{
605 struct jump_entry *iter_start = mod->jump_entries;
606 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
607 struct jump_entry *iter;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100608 struct static_key *key = NULL;
609 struct static_key_mod *jlm, **prev;
Jason Barond430d3d2011-03-16 17:29:47 -0400610
611 for (iter = iter_start; iter < iter_stop; iter++) {
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200612 if (jump_entry_key(iter) == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400613 continue;
614
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200615 key = jump_entry_key(iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400616
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930617 if (within_module(iter->key, mod))
Jason Barond430d3d2011-03-16 17:29:47 -0400618 continue;
619
Jason Baron3821fd32017-02-03 15:42:24 -0500620 /* No memory during module load */
621 if (WARN_ON(!static_key_linked(key)))
622 continue;
623
Jason Barond430d3d2011-03-16 17:29:47 -0400624 prev = &key->next;
Jason Baron3821fd32017-02-03 15:42:24 -0500625 jlm = static_key_mod(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400626
627 while (jlm && jlm->mod != mod) {
628 prev = &jlm->next;
629 jlm = jlm->next;
630 }
631
Jason Baron3821fd32017-02-03 15:42:24 -0500632 /* No memory during module load */
633 if (WARN_ON(!jlm))
634 continue;
635
636 if (prev == &key->next)
637 static_key_set_mod(key, jlm->next);
638 else
Jason Barond430d3d2011-03-16 17:29:47 -0400639 *prev = jlm->next;
Jason Baron3821fd32017-02-03 15:42:24 -0500640
641 kfree(jlm);
642
643 jlm = static_key_mod(key);
644 /* if only one etry is left, fold it back into the static_key */
645 if (jlm->next == NULL) {
646 static_key_set_entries(key, jlm->entries);
647 static_key_clear_linked(key);
Jason Barond430d3d2011-03-16 17:29:47 -0400648 kfree(jlm);
649 }
650 }
651}
652
Josh Poimboeuf33352242018-02-20 11:37:51 -0600653/* Disable any jump label entries in module init code */
Jason Barond430d3d2011-03-16 17:29:47 -0400654static void jump_label_invalidate_module_init(struct module *mod)
655{
656 struct jump_entry *iter_start = mod->jump_entries;
657 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
658 struct jump_entry *iter;
659
660 for (iter = iter_start; iter < iter_stop; iter++) {
661 if (within_module_init(iter->code, mod))
662 iter->code = 0;
663 }
664}
665
666static int
667jump_label_module_notify(struct notifier_block *self, unsigned long val,
668 void *data)
669{
670 struct module *mod = data;
671 int ret = 0;
672
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200673 cpus_read_lock();
674 jump_label_lock();
675
Jason Barond430d3d2011-03-16 17:29:47 -0400676 switch (val) {
677 case MODULE_STATE_COMING:
Jason Barond430d3d2011-03-16 17:29:47 -0400678 ret = jump_label_add_module(mod);
Jason Baron3821fd32017-02-03 15:42:24 -0500679 if (ret) {
680 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
Jason Barond430d3d2011-03-16 17:29:47 -0400681 jump_label_del_module(mod);
Jason Baron3821fd32017-02-03 15:42:24 -0500682 }
Jason Barond430d3d2011-03-16 17:29:47 -0400683 break;
684 case MODULE_STATE_GOING:
Jason Barond430d3d2011-03-16 17:29:47 -0400685 jump_label_del_module(mod);
Jason Barond430d3d2011-03-16 17:29:47 -0400686 break;
687 case MODULE_STATE_LIVE:
Jason Barond430d3d2011-03-16 17:29:47 -0400688 jump_label_invalidate_module_init(mod);
Jason Barond430d3d2011-03-16 17:29:47 -0400689 break;
690 }
691
Thomas Gleixnerf2545b2d2017-05-24 10:15:35 +0200692 jump_label_unlock();
693 cpus_read_unlock();
694
Jason Barond430d3d2011-03-16 17:29:47 -0400695 return notifier_from_errno(ret);
696}
697
Wei Yongjun885885f2016-06-17 17:19:40 +0000698static struct notifier_block jump_label_module_nb = {
Jason Barond430d3d2011-03-16 17:29:47 -0400699 .notifier_call = jump_label_module_notify,
700 .priority = 1, /* higher than tracepoints */
701};
702
703static __init int jump_label_init_module(void)
704{
705 return register_module_notifier(&jump_label_module_nb);
706}
707early_initcall(jump_label_init_module);
708
709#endif /* CONFIG_MODULES */
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400710
711/***
712 * jump_label_text_reserved - check if addr range is reserved
713 * @start: start text addr
714 * @end: end text addr
715 *
716 * checks if the text addr located between @start and @end
717 * overlaps with any of the jump label patch addresses. Code
718 * that wants to modify kernel text should first verify that
719 * it does not overlap with any of the jump label addresses.
Jason Baron91bad2f2010-10-01 17:23:48 -0400720 * Caller must hold jump_label_mutex.
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400721 *
722 * returns 1 if there is an overlap, 0 otherwise
723 */
724int jump_label_text_reserved(void *start, void *end)
725{
Jason Barond430d3d2011-03-16 17:29:47 -0400726 int ret = __jump_label_text_reserved(__start___jump_table,
727 __stop___jump_table, start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400728
Jason Barond430d3d2011-03-16 17:29:47 -0400729 if (ret)
730 return ret;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400731
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400732#ifdef CONFIG_MODULES
Jason Barond430d3d2011-03-16 17:29:47 -0400733 ret = __jump_label_mod_text_reserved(start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400734#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -0400735 return ret;
736}
Jason Barond430d3d2011-03-16 17:29:47 -0400737
Peter Zijlstra706249c2015-07-24 15:06:37 +0200738static void jump_label_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400739{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100740 struct jump_entry *stop = __stop___jump_table;
Jason Baron3821fd32017-02-03 15:42:24 -0500741 struct jump_entry *entry;
Jason Baronbf5438fc2010-09-17 11:09:00 -0400742#ifdef CONFIG_MODULES
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930743 struct module *mod;
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800744
Jason Baron3821fd32017-02-03 15:42:24 -0500745 if (static_key_linked(key)) {
746 __jump_label_mod_update(key);
747 return;
748 }
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800749
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930750 preempt_disable();
751 mod = __module_address((unsigned long)key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800752 if (mod)
753 stop = mod->jump_entries + mod->num_jump_entries;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930754 preempt_enable();
Jason Barond430d3d2011-03-16 17:29:47 -0400755#endif
Jason Baron3821fd32017-02-03 15:42:24 -0500756 entry = static_key_entries(key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800757 /* if there are no users, entry can be NULL */
758 if (entry)
Peter Zijlstra706249c2015-07-24 15:06:37 +0200759 __jump_label_update(key, entry, stop);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400760}
761
Peter Zijlstra1987c942015-07-27 18:32:09 +0200762#ifdef CONFIG_STATIC_KEYS_SELFTEST
763static DEFINE_STATIC_KEY_TRUE(sk_true);
764static DEFINE_STATIC_KEY_FALSE(sk_false);
765
766static __init int jump_label_test(void)
767{
768 int i;
769
770 for (i = 0; i < 2; i++) {
771 WARN_ON(static_key_enabled(&sk_true.key) != true);
772 WARN_ON(static_key_enabled(&sk_false.key) != false);
773
774 WARN_ON(!static_branch_likely(&sk_true));
775 WARN_ON(!static_branch_unlikely(&sk_true));
776 WARN_ON(static_branch_likely(&sk_false));
777 WARN_ON(static_branch_unlikely(&sk_false));
778
779 static_branch_disable(&sk_true);
780 static_branch_enable(&sk_false);
781
782 WARN_ON(static_key_enabled(&sk_true.key) == true);
783 WARN_ON(static_key_enabled(&sk_false.key) == false);
784
785 WARN_ON(static_branch_likely(&sk_true));
786 WARN_ON(static_branch_unlikely(&sk_true));
787 WARN_ON(!static_branch_likely(&sk_false));
788 WARN_ON(!static_branch_unlikely(&sk_false));
789
790 static_branch_enable(&sk_true);
791 static_branch_disable(&sk_false);
792 }
793
794 return 0;
795}
Jason Baron92ee46e2017-11-13 16:48:47 -0500796early_initcall(jump_label_test);
Peter Zijlstra1987c942015-07-27 18:32:09 +0200797#endif /* STATIC_KEYS_SELFTEST */
798
799#endif /* HAVE_JUMP_LABEL */