diff options
Diffstat (limited to 'kernel/jump_label.c')
-rw-r--r-- | kernel/jump_label.c | 135 |
1 files changed, 76 insertions, 59 deletions
diff --git a/kernel/jump_label.c b/kernel/jump_label.c index 01d3b70fc98..43049192b5e 100644 --- a/kernel/jump_label.c +++ b/kernel/jump_label.c | |||
@@ -12,7 +12,7 @@ | |||
12 | #include <linux/slab.h> | 12 | #include <linux/slab.h> |
13 | #include <linux/sort.h> | 13 | #include <linux/sort.h> |
14 | #include <linux/err.h> | 14 | #include <linux/err.h> |
15 | #include <linux/jump_label.h> | 15 | #include <linux/static_key.h> |
16 | 16 | ||
17 | #ifdef HAVE_JUMP_LABEL | 17 | #ifdef HAVE_JUMP_LABEL |
18 | 18 | ||
@@ -29,11 +29,6 @@ void jump_label_unlock(void) | |||
29 | mutex_unlock(&jump_label_mutex); | 29 | mutex_unlock(&jump_label_mutex); |
30 | } | 30 | } |
31 | 31 | ||
32 | bool jump_label_enabled(struct jump_label_key *key) | ||
33 | { | ||
34 | return !!atomic_read(&key->enabled); | ||
35 | } | ||
36 | |||
37 | static int jump_label_cmp(const void *a, const void *b) | 32 | static int jump_label_cmp(const void *a, const void *b) |
38 | { | 33 | { |
39 | const struct jump_entry *jea = a; | 34 | const struct jump_entry *jea = a; |
@@ -58,56 +53,66 @@ jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) | |||
58 | sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL); | 53 | sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL); |
59 | } | 54 | } |
60 | 55 | ||
61 | static void jump_label_update(struct jump_label_key *key, int enable); | 56 | static void jump_label_update(struct static_key *key, int enable); |
62 | 57 | ||
63 | void jump_label_inc(struct jump_label_key *key) | 58 | void static_key_slow_inc(struct static_key *key) |
64 | { | 59 | { |
65 | if (atomic_inc_not_zero(&key->enabled)) | 60 | if (atomic_inc_not_zero(&key->enabled)) |
66 | return; | 61 | return; |
67 | 62 | ||
68 | jump_label_lock(); | 63 | jump_label_lock(); |
69 | if (atomic_read(&key->enabled) == 0) | 64 | if (atomic_read(&key->enabled) == 0) { |
70 | jump_label_update(key, JUMP_LABEL_ENABLE); | 65 | if (!jump_label_get_branch_default(key)) |
66 | jump_label_update(key, JUMP_LABEL_ENABLE); | ||
67 | else | ||
68 | jump_label_update(key, JUMP_LABEL_DISABLE); | ||
69 | } | ||
71 | atomic_inc(&key->enabled); | 70 | atomic_inc(&key->enabled); |
72 | jump_label_unlock(); | 71 | jump_label_unlock(); |
73 | } | 72 | } |
74 | EXPORT_SYMBOL_GPL(jump_label_inc); | 73 | EXPORT_SYMBOL_GPL(static_key_slow_inc); |
75 | 74 | ||
76 | static void __jump_label_dec(struct jump_label_key *key, | 75 | static void __static_key_slow_dec(struct static_key *key, |
77 | unsigned long rate_limit, struct delayed_work *work) | 76 | unsigned long rate_limit, struct delayed_work *work) |
78 | { | 77 | { |
79 | if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) | 78 | if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) { |
79 | WARN(atomic_read(&key->enabled) < 0, | ||
80 | "jump label: negative count!\n"); | ||
80 | return; | 81 | return; |
82 | } | ||
81 | 83 | ||
82 | if (rate_limit) { | 84 | if (rate_limit) { |
83 | atomic_inc(&key->enabled); | 85 | atomic_inc(&key->enabled); |
84 | schedule_delayed_work(work, rate_limit); | 86 | schedule_delayed_work(work, rate_limit); |
85 | } else | 87 | } else { |
86 | jump_label_update(key, JUMP_LABEL_DISABLE); | 88 | if (!jump_label_get_branch_default(key)) |
87 | 89 | jump_label_update(key, JUMP_LABEL_DISABLE); | |
90 | else | ||
91 | jump_label_update(key, JUMP_LABEL_ENABLE); | ||
92 | } | ||
88 | jump_label_unlock(); | 93 | jump_label_unlock(); |
89 | } | 94 | } |
90 | EXPORT_SYMBOL_GPL(jump_label_dec); | ||
91 | 95 | ||
92 | static void jump_label_update_timeout(struct work_struct *work) | 96 | static void jump_label_update_timeout(struct work_struct *work) |
93 | { | 97 | { |
94 | struct jump_label_key_deferred *key = | 98 | struct static_key_deferred *key = |
95 | container_of(work, struct jump_label_key_deferred, work.work); | 99 | container_of(work, struct static_key_deferred, work.work); |
96 | __jump_label_dec(&key->key, 0, NULL); | 100 | __static_key_slow_dec(&key->key, 0, NULL); |
97 | } | 101 | } |
98 | 102 | ||
99 | void jump_label_dec(struct jump_label_key *key) | 103 | void static_key_slow_dec(struct static_key *key) |
100 | { | 104 | { |
101 | __jump_label_dec(key, 0, NULL); | 105 | __static_key_slow_dec(key, 0, NULL); |
102 | } | 106 | } |
107 | EXPORT_SYMBOL_GPL(static_key_slow_dec); | ||
103 | 108 | ||
104 | void jump_label_dec_deferred(struct jump_label_key_deferred *key) | 109 | void static_key_slow_dec_deferred(struct static_key_deferred *key) |
105 | { | 110 | { |
106 | __jump_label_dec(&key->key, key->timeout, &key->work); | 111 | __static_key_slow_dec(&key->key, key->timeout, &key->work); |
107 | } | 112 | } |
113 | EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred); | ||
108 | 114 | ||
109 | 115 | void jump_label_rate_limit(struct static_key_deferred *key, | |
110 | void jump_label_rate_limit(struct jump_label_key_deferred *key, | ||
111 | unsigned long rl) | 116 | unsigned long rl) |
112 | { | 117 | { |
113 | key->timeout = rl; | 118 | key->timeout = rl; |
@@ -150,7 +155,7 @@ void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry | |||
150 | arch_jump_label_transform(entry, type); | 155 | arch_jump_label_transform(entry, type); |
151 | } | 156 | } |
152 | 157 | ||
153 | static void __jump_label_update(struct jump_label_key *key, | 158 | static void __jump_label_update(struct static_key *key, |
154 | struct jump_entry *entry, | 159 | struct jump_entry *entry, |
155 | struct jump_entry *stop, int enable) | 160 | struct jump_entry *stop, int enable) |
156 | { | 161 | { |
@@ -167,27 +172,40 @@ static void __jump_label_update(struct jump_label_key *key, | |||
167 | } | 172 | } |
168 | } | 173 | } |
169 | 174 | ||
175 | static enum jump_label_type jump_label_type(struct static_key *key) | ||
176 | { | ||
177 | bool true_branch = jump_label_get_branch_default(key); | ||
178 | bool state = static_key_enabled(key); | ||
179 | |||
180 | if ((!true_branch && state) || (true_branch && !state)) | ||
181 | return JUMP_LABEL_ENABLE; | ||
182 | |||
183 | return JUMP_LABEL_DISABLE; | ||
184 | } | ||
185 | |||
170 | void __init jump_label_init(void) | 186 | void __init jump_label_init(void) |
171 | { | 187 | { |
172 | struct jump_entry *iter_start = __start___jump_table; | 188 | struct jump_entry *iter_start = __start___jump_table; |
173 | struct jump_entry *iter_stop = __stop___jump_table; | 189 | struct jump_entry *iter_stop = __stop___jump_table; |
174 | struct jump_label_key *key = NULL; | 190 | struct static_key *key = NULL; |
175 | struct jump_entry *iter; | 191 | struct jump_entry *iter; |
176 | 192 | ||
177 | jump_label_lock(); | 193 | jump_label_lock(); |
178 | jump_label_sort_entries(iter_start, iter_stop); | 194 | jump_label_sort_entries(iter_start, iter_stop); |
179 | 195 | ||
180 | for (iter = iter_start; iter < iter_stop; iter++) { | 196 | for (iter = iter_start; iter < iter_stop; iter++) { |
181 | struct jump_label_key *iterk; | 197 | struct static_key *iterk; |
182 | 198 | ||
183 | iterk = (struct jump_label_key *)(unsigned long)iter->key; | 199 | iterk = (struct static_key *)(unsigned long)iter->key; |
184 | arch_jump_label_transform_static(iter, jump_label_enabled(iterk) ? | 200 | arch_jump_label_transform_static(iter, jump_label_type(iterk)); |
185 | JUMP_LABEL_ENABLE : JUMP_LABEL_DISABLE); | ||
186 | if (iterk == key) | 201 | if (iterk == key) |
187 | continue; | 202 | continue; |
188 | 203 | ||
189 | key = iterk; | 204 | key = iterk; |
190 | key->entries = iter; | 205 | /* |
206 | * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH. | ||
207 | */ | ||
208 | *((unsigned long *)&key->entries) += (unsigned long)iter; | ||
191 | #ifdef CONFIG_MODULES | 209 | #ifdef CONFIG_MODULES |
192 | key->next = NULL; | 210 | key->next = NULL; |
193 | #endif | 211 | #endif |
@@ -197,8 +215,8 @@ void __init jump_label_init(void) | |||
197 | 215 | ||
198 | #ifdef CONFIG_MODULES | 216 | #ifdef CONFIG_MODULES |
199 | 217 | ||
200 | struct jump_label_mod { | 218 | struct static_key_mod { |
201 | struct jump_label_mod *next; | 219 | struct static_key_mod *next; |
202 | struct jump_entry *entries; | 220 | struct jump_entry *entries; |
203 | struct module *mod; | 221 | struct module *mod; |
204 | }; | 222 | }; |
@@ -218,9 +236,9 @@ static int __jump_label_mod_text_reserved(void *start, void *end) | |||
218 | start, end); | 236 | start, end); |
219 | } | 237 | } |
220 | 238 | ||
221 | static void __jump_label_mod_update(struct jump_label_key *key, int enable) | 239 | static void __jump_label_mod_update(struct static_key *key, int enable) |
222 | { | 240 | { |
223 | struct jump_label_mod *mod = key->next; | 241 | struct static_key_mod *mod = key->next; |
224 | 242 | ||
225 | while (mod) { | 243 | while (mod) { |
226 | struct module *m = mod->mod; | 244 | struct module *m = mod->mod; |
@@ -251,11 +269,7 @@ void jump_label_apply_nops(struct module *mod) | |||
251 | return; | 269 | return; |
252 | 270 | ||
253 | for (iter = iter_start; iter < iter_stop; iter++) { | 271 | for (iter = iter_start; iter < iter_stop; iter++) { |
254 | struct jump_label_key *iterk; | 272 | arch_jump_label_transform_static(iter, JUMP_LABEL_DISABLE); |
255 | |||
256 | iterk = (struct jump_label_key *)(unsigned long)iter->key; | ||
257 | arch_jump_label_transform_static(iter, jump_label_enabled(iterk) ? | ||
258 | JUMP_LABEL_ENABLE : JUMP_LABEL_DISABLE); | ||
259 | } | 273 | } |
260 | } | 274 | } |
261 | 275 | ||
@@ -264,8 +278,8 @@ static int jump_label_add_module(struct module *mod) | |||
264 | struct jump_entry *iter_start = mod->jump_entries; | 278 | struct jump_entry *iter_start = mod->jump_entries; |
265 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; | 279 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
266 | struct jump_entry *iter; | 280 | struct jump_entry *iter; |
267 | struct jump_label_key *key = NULL; | 281 | struct static_key *key = NULL; |
268 | struct jump_label_mod *jlm; | 282 | struct static_key_mod *jlm; |
269 | 283 | ||
270 | /* if the module doesn't have jump label entries, just return */ | 284 | /* if the module doesn't have jump label entries, just return */ |
271 | if (iter_start == iter_stop) | 285 | if (iter_start == iter_stop) |
@@ -274,28 +288,30 @@ static int jump_label_add_module(struct module *mod) | |||
274 | jump_label_sort_entries(iter_start, iter_stop); | 288 | jump_label_sort_entries(iter_start, iter_stop); |
275 | 289 | ||
276 | for (iter = iter_start; iter < iter_stop; iter++) { | 290 | for (iter = iter_start; iter < iter_stop; iter++) { |
277 | if (iter->key == (jump_label_t)(unsigned long)key) | 291 | struct static_key *iterk; |
278 | continue; | ||
279 | 292 | ||
280 | key = (struct jump_label_key *)(unsigned long)iter->key; | 293 | iterk = (struct static_key *)(unsigned long)iter->key; |
294 | if (iterk == key) | ||
295 | continue; | ||
281 | 296 | ||
297 | key = iterk; | ||
282 | if (__module_address(iter->key) == mod) { | 298 | if (__module_address(iter->key) == mod) { |
283 | atomic_set(&key->enabled, 0); | 299 | /* |
284 | key->entries = iter; | 300 | * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH. |
301 | */ | ||
302 | *((unsigned long *)&key->entries) += (unsigned long)iter; | ||
285 | key->next = NULL; | 303 | key->next = NULL; |
286 | continue; | 304 | continue; |
287 | } | 305 | } |
288 | 306 | jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL); | |
289 | jlm = kzalloc(sizeof(struct jump_label_mod), GFP_KERNEL); | ||
290 | if (!jlm) | 307 | if (!jlm) |
291 | return -ENOMEM; | 308 | return -ENOMEM; |
292 | |||
293 | jlm->mod = mod; | 309 | jlm->mod = mod; |
294 | jlm->entries = iter; | 310 | jlm->entries = iter; |
295 | jlm->next = key->next; | 311 | jlm->next = key->next; |
296 | key->next = jlm; | 312 | key->next = jlm; |
297 | 313 | ||
298 | if (jump_label_enabled(key)) | 314 | if (jump_label_type(key) == JUMP_LABEL_ENABLE) |
299 | __jump_label_update(key, iter, iter_stop, JUMP_LABEL_ENABLE); | 315 | __jump_label_update(key, iter, iter_stop, JUMP_LABEL_ENABLE); |
300 | } | 316 | } |
301 | 317 | ||
@@ -307,14 +323,14 @@ static void jump_label_del_module(struct module *mod) | |||
307 | struct jump_entry *iter_start = mod->jump_entries; | 323 | struct jump_entry *iter_start = mod->jump_entries; |
308 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; | 324 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; |
309 | struct jump_entry *iter; | 325 | struct jump_entry *iter; |
310 | struct jump_label_key *key = NULL; | 326 | struct static_key *key = NULL; |
311 | struct jump_label_mod *jlm, **prev; | 327 | struct static_key_mod *jlm, **prev; |
312 | 328 | ||
313 | for (iter = iter_start; iter < iter_stop; iter++) { | 329 | for (iter = iter_start; iter < iter_stop; iter++) { |
314 | if (iter->key == (jump_label_t)(unsigned long)key) | 330 | if (iter->key == (jump_label_t)(unsigned long)key) |
315 | continue; | 331 | continue; |
316 | 332 | ||
317 | key = (struct jump_label_key *)(unsigned long)iter->key; | 333 | key = (struct static_key *)(unsigned long)iter->key; |
318 | 334 | ||
319 | if (__module_address(iter->key) == mod) | 335 | if (__module_address(iter->key) == mod) |
320 | continue; | 336 | continue; |
@@ -416,12 +432,13 @@ int jump_label_text_reserved(void *start, void *end) | |||
416 | return ret; | 432 | return ret; |
417 | } | 433 | } |
418 | 434 | ||
419 | static void jump_label_update(struct jump_label_key *key, int enable) | 435 | static void jump_label_update(struct static_key *key, int enable) |
420 | { | 436 | { |
421 | struct jump_entry *entry = key->entries, *stop = __stop___jump_table; | 437 | struct jump_entry *stop = __stop___jump_table; |
438 | struct jump_entry *entry = jump_label_get_entries(key); | ||
422 | 439 | ||
423 | #ifdef CONFIG_MODULES | 440 | #ifdef CONFIG_MODULES |
424 | struct module *mod = __module_address((jump_label_t)key); | 441 | struct module *mod = __module_address((unsigned long)key); |
425 | 442 | ||
426 | __jump_label_mod_update(key, enable); | 443 | __jump_label_mod_update(key, enable); |
427 | 444 | ||