blob: 5c8b43b816af06cefcb62c36ddd630ca6e8d54cd [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>
Josh Poimboeufbfd08c52018-03-19 13:18:57 -050018#include <asm/sections.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
Jason Baron91bad2f2010-10-01 17:23:48 -0400128 jump_label_lock();
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200129 if (atomic_read(&key->enabled) == 0) {
130 atomic_set(&key->enabled, -1);
Peter Zijlstra706249c2015-07-24 15:06:37 +0200131 jump_label_update(key);
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200132 atomic_set(&key->enabled, 1);
133 } else {
134 atomic_inc(&key->enabled);
135 }
Jason Barond430d3d2011-03-16 17:29:47 -0400136 jump_label_unlock();
137}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100138EXPORT_SYMBOL_GPL(static_key_slow_inc);
Jason Barond430d3d2011-03-16 17:29:47 -0400139
Ingo Molnarc5905af2012-02-24 08:31:31 +0100140static void __static_key_slow_dec(struct static_key *key,
Gleb Natapovb2029522011-11-27 17:59:09 +0200141 unsigned long rate_limit, struct delayed_work *work)
Jason Barond430d3d2011-03-16 17:29:47 -0400142{
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200143 /*
144 * The negative count check is valid even when a negative
145 * key->enabled is in use by static_key_slow_inc(); a
146 * __static_key_slow_dec() before the first static_key_slow_inc()
147 * returns is unbalanced, because all other static_key_slow_inc()
148 * instances block while the update is in progress.
149 */
Jason Baronfadf0462012-02-21 15:02:53 -0500150 if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
151 WARN(atomic_read(&key->enabled) < 0,
152 "jump label: negative count!\n");
Jason Barond430d3d2011-03-16 17:29:47 -0400153 return;
Jason Baronfadf0462012-02-21 15:02:53 -0500154 }
Jason Barond430d3d2011-03-16 17:29:47 -0400155
Gleb Natapovb2029522011-11-27 17:59:09 +0200156 if (rate_limit) {
157 atomic_inc(&key->enabled);
158 schedule_delayed_work(work, rate_limit);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100159 } else {
Peter Zijlstra706249c2015-07-24 15:06:37 +0200160 jump_label_update(key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100161 }
Jason Baron91bad2f2010-10-01 17:23:48 -0400162 jump_label_unlock();
Jason Baronbf5438fc2010-09-17 11:09:00 -0400163}
164
Gleb Natapovb2029522011-11-27 17:59:09 +0200165static void jump_label_update_timeout(struct work_struct *work)
166{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100167 struct static_key_deferred *key =
168 container_of(work, struct static_key_deferred, work.work);
169 __static_key_slow_dec(&key->key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200170}
171
Ingo Molnarc5905af2012-02-24 08:31:31 +0100172void static_key_slow_dec(struct static_key *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200173{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200174 STATIC_KEY_CHECK_USE();
Ingo Molnarc5905af2012-02-24 08:31:31 +0100175 __static_key_slow_dec(key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200176}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100177EXPORT_SYMBOL_GPL(static_key_slow_dec);
Gleb Natapovb2029522011-11-27 17:59:09 +0200178
Ingo Molnarc5905af2012-02-24 08:31:31 +0100179void static_key_slow_dec_deferred(struct static_key_deferred *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200180{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200181 STATIC_KEY_CHECK_USE();
Ingo Molnarc5905af2012-02-24 08:31:31 +0100182 __static_key_slow_dec(&key->key, key->timeout, &key->work);
Gleb Natapovb2029522011-11-27 17:59:09 +0200183}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100184EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
Gleb Natapovb2029522011-11-27 17:59:09 +0200185
David Matlack483eceb2016-12-16 14:30:35 -0800186void static_key_deferred_flush(struct static_key_deferred *key)
187{
188 STATIC_KEY_CHECK_USE();
189 flush_delayed_work(&key->work);
190}
191EXPORT_SYMBOL_GPL(static_key_deferred_flush);
192
Ingo Molnarc5905af2012-02-24 08:31:31 +0100193void jump_label_rate_limit(struct static_key_deferred *key,
Gleb Natapovb2029522011-11-27 17:59:09 +0200194 unsigned long rl)
195{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200196 STATIC_KEY_CHECK_USE();
Gleb Natapovb2029522011-11-27 17:59:09 +0200197 key->timeout = rl;
198 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
199}
Gleb Natapova181dc12012-08-05 15:58:29 +0300200EXPORT_SYMBOL_GPL(jump_label_rate_limit);
Gleb Natapovb2029522011-11-27 17:59:09 +0200201
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400202static int addr_conflict(struct jump_entry *entry, void *start, void *end)
203{
204 if (entry->code <= (unsigned long)end &&
205 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
206 return 1;
207
208 return 0;
209}
210
Jason Barond430d3d2011-03-16 17:29:47 -0400211static int __jump_label_text_reserved(struct jump_entry *iter_start,
212 struct jump_entry *iter_stop, void *start, void *end)
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400213{
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400214 struct jump_entry *iter;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400215
Jason Barond430d3d2011-03-16 17:29:47 -0400216 iter = iter_start;
217 while (iter < iter_stop) {
218 if (addr_conflict(iter, start, end))
219 return 1;
220 iter++;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400221 }
Jason Barond430d3d2011-03-16 17:29:47 -0400222
223 return 0;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400224}
225
Peter Zijlstra706249c2015-07-24 15:06:37 +0200226/*
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700227 * Update code which is definitely not currently executing.
228 * Architectures which need heavyweight synchronization to modify
229 * running code can override this to make the non-live update case
230 * cheaper.
231 */
Peter Zijlstra9cdbe1c2011-12-06 17:27:29 +0100232void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700233 enum jump_label_type type)
234{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200235 arch_jump_label_transform(entry, type);
Jason Barond430d3d2011-03-16 17:29:47 -0400236}
237
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200238static inline struct jump_entry *static_key_entries(struct static_key *key)
239{
240 return (struct jump_entry *)((unsigned long)key->entries & ~JUMP_TYPE_MASK);
241}
242
Peter Zijlstra706249c2015-07-24 15:06:37 +0200243static inline bool static_key_type(struct static_key *key)
244{
245 return (unsigned long)key->entries & JUMP_TYPE_MASK;
246}
247
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200248static inline struct static_key *jump_entry_key(struct jump_entry *entry)
249{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200250 return (struct static_key *)((unsigned long)entry->key & ~1UL);
251}
252
253static bool jump_entry_branch(struct jump_entry *entry)
254{
255 return (unsigned long)entry->key & 1UL;
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200256}
257
Peter Zijlstra706249c2015-07-24 15:06:37 +0200258static enum jump_label_type jump_label_type(struct jump_entry *entry)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100259{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200260 struct static_key *key = jump_entry_key(entry);
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200261 bool enabled = static_key_enabled(key);
Peter Zijlstra11276d52015-07-24 15:09:55 +0200262 bool branch = jump_entry_branch(entry);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100263
Peter Zijlstra11276d52015-07-24 15:09:55 +0200264 /* See the comment in linux/jump_label.h */
265 return enabled ^ branch;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100266}
267
Peter Zijlstra706249c2015-07-24 15:06:37 +0200268static void __jump_label_update(struct static_key *key,
269 struct jump_entry *entry,
270 struct jump_entry *stop)
271{
272 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
273 /*
Josh Poimboeuf26cbe2c2018-02-20 11:37:52 -0600274 * An entry->code of 0 indicates an entry which has been
275 * disabled because it was in an init text area.
Peter Zijlstra706249c2015-07-24 15:06:37 +0200276 */
Josh Poimboeuf26cbe2c2018-02-20 11:37:52 -0600277 if (entry->code) {
278 if (kernel_text_address(entry->code))
279 arch_jump_label_transform(entry, jump_label_type(entry));
280 else
281 WARN_ONCE(1, "can't patch jump_label at %pS", (void *)entry->code);
282 }
Peter Zijlstra706249c2015-07-24 15:06:37 +0200283 }
284}
285
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700286void __init jump_label_init(void)
Jason Barond430d3d2011-03-16 17:29:47 -0400287{
288 struct jump_entry *iter_start = __start___jump_table;
289 struct jump_entry *iter_stop = __stop___jump_table;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100290 struct static_key *key = NULL;
Jason Barond430d3d2011-03-16 17:29:47 -0400291 struct jump_entry *iter;
292
Jason Baron1f69bf92016-08-03 13:46:36 -0700293 /*
294 * Since we are initializing the static_key.enabled field with
295 * with the 'raw' int values (to avoid pulling in atomic.h) in
296 * jump_label.h, let's make sure that is safe. There are only two
297 * cases to check since we initialize to 0 or 1.
298 */
299 BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
300 BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
301
Kevin Haoe3f91082016-07-23 14:42:37 +0530302 if (static_key_initialized)
303 return;
304
Jason Barond430d3d2011-03-16 17:29:47 -0400305 jump_label_lock();
306 jump_label_sort_entries(iter_start, iter_stop);
307
308 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100309 struct static_key *iterk;
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700310
Peter Zijlstra11276d52015-07-24 15:09:55 +0200311 /* rewrite NOPs */
312 if (jump_label_type(iter) == JUMP_LABEL_NOP)
313 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
314
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200315 iterk = jump_entry_key(iter);
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700316 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400317 continue;
318
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700319 key = iterk;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100320 /*
321 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
322 */
323 *((unsigned long *)&key->entries) += (unsigned long)iter;
Jason Barond430d3d2011-03-16 17:29:47 -0400324#ifdef CONFIG_MODULES
325 key->next = NULL;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400326#endif
Jason Barond430d3d2011-03-16 17:29:47 -0400327 }
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200328 static_key_initialized = true;
Jason Barond430d3d2011-03-16 17:29:47 -0400329 jump_label_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400330}
Jason Barond430d3d2011-03-16 17:29:47 -0400331
Josh Poimboeufbfd08c52018-03-19 13:18:57 -0500332/* Disable any jump label entries in __init/__exit code */
333void __init jump_label_invalidate_initmem(void)
Josh Poimboeuf21fd7752018-02-20 11:37:51 -0600334{
335 struct jump_entry *iter_start = __start___jump_table;
336 struct jump_entry *iter_stop = __stop___jump_table;
337 struct jump_entry *iter;
338
339 for (iter = iter_start; iter < iter_stop; iter++) {
Josh Poimboeufbfd08c52018-03-19 13:18:57 -0500340 if (init_section_contains((void *)(unsigned long)iter->code, 1))
Josh Poimboeuf21fd7752018-02-20 11:37:51 -0600341 iter->code = 0;
342 }
343}
344
Jason Barond430d3d2011-03-16 17:29:47 -0400345#ifdef CONFIG_MODULES
346
Peter Zijlstra11276d52015-07-24 15:09:55 +0200347static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
348{
349 struct static_key *key = jump_entry_key(entry);
350 bool type = static_key_type(key);
351 bool branch = jump_entry_branch(entry);
352
353 /* See the comment in linux/jump_label.h */
354 return type ^ branch;
355}
356
Ingo Molnarc5905af2012-02-24 08:31:31 +0100357struct static_key_mod {
358 struct static_key_mod *next;
Jason Barond430d3d2011-03-16 17:29:47 -0400359 struct jump_entry *entries;
360 struct module *mod;
361};
362
363static int __jump_label_mod_text_reserved(void *start, void *end)
364{
365 struct module *mod;
366
Rusty Russellbdc9f372016-07-27 12:17:35 +0930367 preempt_disable();
Jason Barond430d3d2011-03-16 17:29:47 -0400368 mod = __module_text_address((unsigned long)start);
Rusty Russellbdc9f372016-07-27 12:17:35 +0930369 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
370 preempt_enable();
371
Jason Barond430d3d2011-03-16 17:29:47 -0400372 if (!mod)
373 return 0;
374
Jason Barond430d3d2011-03-16 17:29:47 -0400375
376 return __jump_label_text_reserved(mod->jump_entries,
377 mod->jump_entries + mod->num_jump_entries,
378 start, end);
379}
380
Peter Zijlstra706249c2015-07-24 15:06:37 +0200381static void __jump_label_mod_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400382{
Peter Zijlstra706249c2015-07-24 15:06:37 +0200383 struct static_key_mod *mod;
Jason Barond430d3d2011-03-16 17:29:47 -0400384
Peter Zijlstra706249c2015-07-24 15:06:37 +0200385 for (mod = key->next; mod; mod = mod->next) {
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200386 struct module *m = mod->mod;
387
388 __jump_label_update(key, mod->entries,
Peter Zijlstra706249c2015-07-24 15:06:37 +0200389 m->jump_entries + m->num_jump_entries);
Jason Barond430d3d2011-03-16 17:29:47 -0400390 }
391}
392
393/***
394 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
395 * @mod: module to patch
396 *
397 * Allow for run-time selection of the optimal nops. Before the module
398 * loads patch these with arch_get_jump_label_nop(), which is specified by
399 * the arch specific jump label code.
400 */
401void jump_label_apply_nops(struct module *mod)
402{
403 struct jump_entry *iter_start = mod->jump_entries;
404 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
405 struct jump_entry *iter;
406
407 /* if the module doesn't have jump label entries, just return */
408 if (iter_start == iter_stop)
409 return;
410
Peter Zijlstra11276d52015-07-24 15:09:55 +0200411 for (iter = iter_start; iter < iter_stop; iter++) {
412 /* Only write NOPs for arch_branch_static(). */
413 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
414 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
415 }
Jason Barond430d3d2011-03-16 17:29:47 -0400416}
417
418static int jump_label_add_module(struct module *mod)
419{
420 struct jump_entry *iter_start = mod->jump_entries;
421 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
422 struct jump_entry *iter;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100423 struct static_key *key = NULL;
424 struct static_key_mod *jlm;
Jason Barond430d3d2011-03-16 17:29:47 -0400425
426 /* if the module doesn't have jump label entries, just return */
427 if (iter_start == iter_stop)
428 return 0;
429
430 jump_label_sort_entries(iter_start, iter_stop);
431
432 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100433 struct static_key *iterk;
434
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200435 iterk = jump_entry_key(iter);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100436 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400437 continue;
438
Ingo Molnarc5905af2012-02-24 08:31:31 +0100439 key = iterk;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930440 if (within_module(iter->key, mod)) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100441 /*
442 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
443 */
444 *((unsigned long *)&key->entries) += (unsigned long)iter;
Jason Barond430d3d2011-03-16 17:29:47 -0400445 key->next = NULL;
446 continue;
447 }
Ingo Molnarc5905af2012-02-24 08:31:31 +0100448 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
Jason Barond430d3d2011-03-16 17:29:47 -0400449 if (!jlm)
450 return -ENOMEM;
Jason Barond430d3d2011-03-16 17:29:47 -0400451 jlm->mod = mod;
452 jlm->entries = iter;
453 jlm->next = key->next;
454 key->next = jlm;
455
Peter Zijlstra11276d52015-07-24 15:09:55 +0200456 /* Only update if we've changed from our initial state */
457 if (jump_label_type(iter) != jump_label_init_type(iter))
Peter Zijlstra706249c2015-07-24 15:06:37 +0200458 __jump_label_update(key, iter, iter_stop);
Jason Barond430d3d2011-03-16 17:29:47 -0400459 }
460
461 return 0;
462}
463
464static void jump_label_del_module(struct module *mod)
465{
466 struct jump_entry *iter_start = mod->jump_entries;
467 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
468 struct jump_entry *iter;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100469 struct static_key *key = NULL;
470 struct static_key_mod *jlm, **prev;
Jason Barond430d3d2011-03-16 17:29:47 -0400471
472 for (iter = iter_start; iter < iter_stop; iter++) {
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200473 if (jump_entry_key(iter) == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400474 continue;
475
Peter Zijlstra7dcfd912015-07-24 15:02:27 +0200476 key = jump_entry_key(iter);
Jason Barond430d3d2011-03-16 17:29:47 -0400477
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930478 if (within_module(iter->key, mod))
Jason Barond430d3d2011-03-16 17:29:47 -0400479 continue;
480
481 prev = &key->next;
482 jlm = key->next;
483
484 while (jlm && jlm->mod != mod) {
485 prev = &jlm->next;
486 jlm = jlm->next;
487 }
488
489 if (jlm) {
490 *prev = jlm->next;
491 kfree(jlm);
492 }
493 }
494}
495
Josh Poimboeuf21fd7752018-02-20 11:37:51 -0600496/* Disable any jump label entries in module init code */
Jason Barond430d3d2011-03-16 17:29:47 -0400497static void jump_label_invalidate_module_init(struct module *mod)
498{
499 struct jump_entry *iter_start = mod->jump_entries;
500 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
501 struct jump_entry *iter;
502
503 for (iter = iter_start; iter < iter_stop; iter++) {
504 if (within_module_init(iter->code, mod))
505 iter->code = 0;
506 }
507}
508
509static int
510jump_label_module_notify(struct notifier_block *self, unsigned long val,
511 void *data)
512{
513 struct module *mod = data;
514 int ret = 0;
515
516 switch (val) {
517 case MODULE_STATE_COMING:
518 jump_label_lock();
519 ret = jump_label_add_module(mod);
520 if (ret)
521 jump_label_del_module(mod);
522 jump_label_unlock();
523 break;
524 case MODULE_STATE_GOING:
525 jump_label_lock();
526 jump_label_del_module(mod);
527 jump_label_unlock();
528 break;
529 case MODULE_STATE_LIVE:
530 jump_label_lock();
531 jump_label_invalidate_module_init(mod);
532 jump_label_unlock();
533 break;
534 }
535
536 return notifier_from_errno(ret);
537}
538
Wei Yongjun885885f2016-06-17 17:19:40 +0000539static struct notifier_block jump_label_module_nb = {
Jason Barond430d3d2011-03-16 17:29:47 -0400540 .notifier_call = jump_label_module_notify,
541 .priority = 1, /* higher than tracepoints */
542};
543
544static __init int jump_label_init_module(void)
545{
546 return register_module_notifier(&jump_label_module_nb);
547}
548early_initcall(jump_label_init_module);
549
550#endif /* CONFIG_MODULES */
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400551
552/***
553 * jump_label_text_reserved - check if addr range is reserved
554 * @start: start text addr
555 * @end: end text addr
556 *
557 * checks if the text addr located between @start and @end
558 * overlaps with any of the jump label patch addresses. Code
559 * that wants to modify kernel text should first verify that
560 * it does not overlap with any of the jump label addresses.
Jason Baron91bad2f2010-10-01 17:23:48 -0400561 * Caller must hold jump_label_mutex.
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400562 *
563 * returns 1 if there is an overlap, 0 otherwise
564 */
565int jump_label_text_reserved(void *start, void *end)
566{
Jason Barond430d3d2011-03-16 17:29:47 -0400567 int ret = __jump_label_text_reserved(__start___jump_table,
568 __stop___jump_table, start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400569
Jason Barond430d3d2011-03-16 17:29:47 -0400570 if (ret)
571 return ret;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400572
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400573#ifdef CONFIG_MODULES
Jason Barond430d3d2011-03-16 17:29:47 -0400574 ret = __jump_label_mod_text_reserved(start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400575#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -0400576 return ret;
577}
Jason Barond430d3d2011-03-16 17:29:47 -0400578
Peter Zijlstra706249c2015-07-24 15:06:37 +0200579static void jump_label_update(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400580{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100581 struct jump_entry *stop = __stop___jump_table;
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200582 struct jump_entry *entry = static_key_entries(key);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400583#ifdef CONFIG_MODULES
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930584 struct module *mod;
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800585
Peter Zijlstra706249c2015-07-24 15:06:37 +0200586 __jump_label_mod_update(key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800587
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930588 preempt_disable();
589 mod = __module_address((unsigned long)key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800590 if (mod)
591 stop = mod->jump_entries + mod->num_jump_entries;
Peter Zijlstrabed831f2015-05-27 11:09:35 +0930592 preempt_enable();
Jason Barond430d3d2011-03-16 17:29:47 -0400593#endif
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800594 /* if there are no users, entry can be NULL */
595 if (entry)
Peter Zijlstra706249c2015-07-24 15:06:37 +0200596 __jump_label_update(key, entry, stop);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400597}
598
Peter Zijlstra1987c942015-07-27 18:32:09 +0200599#ifdef CONFIG_STATIC_KEYS_SELFTEST
600static DEFINE_STATIC_KEY_TRUE(sk_true);
601static DEFINE_STATIC_KEY_FALSE(sk_false);
602
603static __init int jump_label_test(void)
604{
605 int i;
606
607 for (i = 0; i < 2; i++) {
608 WARN_ON(static_key_enabled(&sk_true.key) != true);
609 WARN_ON(static_key_enabled(&sk_false.key) != false);
610
611 WARN_ON(!static_branch_likely(&sk_true));
612 WARN_ON(!static_branch_unlikely(&sk_true));
613 WARN_ON(static_branch_likely(&sk_false));
614 WARN_ON(static_branch_unlikely(&sk_false));
615
616 static_branch_disable(&sk_true);
617 static_branch_enable(&sk_false);
618
619 WARN_ON(static_key_enabled(&sk_true.key) == true);
620 WARN_ON(static_key_enabled(&sk_false.key) == false);
621
622 WARN_ON(static_branch_likely(&sk_true));
623 WARN_ON(static_branch_unlikely(&sk_true));
624 WARN_ON(!static_branch_likely(&sk_false));
625 WARN_ON(!static_branch_unlikely(&sk_false));
626
627 static_branch_enable(&sk_true);
628 static_branch_disable(&sk_false);
629 }
630
631 return 0;
632}
Jason Baron74b470c2017-11-13 16:48:47 -0500633early_initcall(jump_label_test);
Peter Zijlstra1987c942015-07-27 18:32:09 +0200634#endif /* STATIC_KEYS_SELFTEST */
635
636#endif /* HAVE_JUMP_LABEL */