aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/jump_label.c
diff options
context:
space:
mode:
authorJason Baron <jbaron@akamai.com>2017-02-03 15:42:24 -0500
committerSteven Rostedt (VMware) <rostedt@goodmis.org>2017-02-15 09:02:26 -0500
commit3821fd35b58dba449bd894014fbf4e1c43c9e951 (patch)
tree1cda103a9fd67edb26a225ce1679ac3c83595903 /kernel/jump_label.c
parent7257634135c247de235f3cdfdaa22f9eb5f054e4 (diff)
jump_label: Reduce the size of struct static_key
The static_key->next field goes mostly unused. The field is used for associating module uses with a static key. Most uses of struct static_key define a static key in the core kernel and make use of it entirely within the core kernel, or define the static key in a module and make use of it only from within that module. In fact, of the ~3,000 static keys defined, I found only about 5 or so that did not fit this pattern. Thus, we can remove the static_key->next field entirely and overload the static_key->entries field. That is, when all the static_key uses are contained within the same module, static_key->entries continues to point to those uses. However, if the static_key uses are not contained within the module where the static_key is defined, then we allocate a struct static_key_mod, store a pointer to the uses within that struct static_key_mod, and have the static key point at the static_key_mod. This does incur some extra memory usage when a static_key is used in a module that does not define it, but since there are only a handful of such cases there is a net savings. In order to identify if the static_key->entries pointer contains a struct static_key_mod or a struct jump_entry pointer, bit 1 of static_key->entries is set to 1 if it points to a struct static_key_mod and is 0 if it points to a struct jump_entry. We were already using bit 0 in a similar way to store the initial value of the static_key. This does mean that allocations of struct static_key_mod and that the struct jump_entry tables need to be at least 4-byte aligned in memory. As far as I can tell all arches meet this criteria. For my .config, the patch increased the text by 778 bytes, but reduced the data + bss size by 14912, for a net savings of 14,134 bytes. text data bss dec hex filename 8092427 5016512 790528 13899467 d416cb vmlinux.pre 8093205 5001600 790528 13885333 d3df95 vmlinux.post Link: http://lkml.kernel.org/r/1486154544-4321-1-git-send-email-jbaron@akamai.com Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@kernel.org> Cc: Joe Perches <joe@perches.com> Signed-off-by: Jason Baron <jbaron@akamai.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Diffstat (limited to 'kernel/jump_label.c')
-rw-r--r--kernel/jump_label.c153
1 files changed, 127 insertions, 26 deletions
diff --git a/kernel/jump_label.c b/kernel/jump_label.c
index 93ad6c1fb9b6..953411f5ba7f 100644
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -229,12 +229,28 @@ void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry
229 229
230static inline struct jump_entry *static_key_entries(struct static_key *key) 230static inline struct jump_entry *static_key_entries(struct static_key *key)
231{ 231{
232 return (struct jump_entry *)((unsigned long)key->entries & ~JUMP_TYPE_MASK); 232 WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
233 return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
233} 234}
234 235
235static inline bool static_key_type(struct static_key *key) 236static inline bool static_key_type(struct static_key *key)
236{ 237{
237 return (unsigned long)key->entries & JUMP_TYPE_MASK; 238 return key->type & JUMP_TYPE_TRUE;
239}
240
241static inline bool static_key_linked(struct static_key *key)
242{
243 return key->type & JUMP_TYPE_LINKED;
244}
245
246static inline void static_key_clear_linked(struct static_key *key)
247{
248 key->type &= ~JUMP_TYPE_LINKED;
249}
250
251static inline void static_key_set_linked(struct static_key *key)
252{
253 key->type |= JUMP_TYPE_LINKED;
238} 254}
239 255
240static inline struct static_key *jump_entry_key(struct jump_entry *entry) 256static inline struct static_key *jump_entry_key(struct jump_entry *entry)
@@ -247,6 +263,26 @@ static bool jump_entry_branch(struct jump_entry *entry)
247 return (unsigned long)entry->key & 1UL; 263 return (unsigned long)entry->key & 1UL;
248} 264}
249 265
266/***
267 * A 'struct static_key' uses a union such that it either points directly
268 * to a table of 'struct jump_entry' or to a linked list of modules which in
269 * turn point to 'struct jump_entry' tables.
270 *
271 * The two lower bits of the pointer are used to keep track of which pointer
272 * type is in use and to store the initial branch direction, we use an access
273 * function which preserves these bits.
274 */
275static void static_key_set_entries(struct static_key *key,
276 struct jump_entry *entries)
277{
278 unsigned long type;
279
280 WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
281 type = key->type & JUMP_TYPE_MASK;
282 key->entries = entries;
283 key->type |= type;
284}
285
250static enum jump_label_type jump_label_type(struct jump_entry *entry) 286static enum jump_label_type jump_label_type(struct jump_entry *entry)
251{ 287{
252 struct static_key *key = jump_entry_key(entry); 288 struct static_key *key = jump_entry_key(entry);
@@ -306,13 +342,7 @@ void __init jump_label_init(void)
306 continue; 342 continue;
307 343
308 key = iterk; 344 key = iterk;
309 /* 345 static_key_set_entries(key, iter);
310 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
311 */
312 *((unsigned long *)&key->entries) += (unsigned long)iter;
313#ifdef CONFIG_MODULES
314 key->next = NULL;
315#endif
316 } 346 }
317 static_key_initialized = true; 347 static_key_initialized = true;
318 jump_label_unlock(); 348 jump_label_unlock();
@@ -336,6 +366,29 @@ struct static_key_mod {
336 struct module *mod; 366 struct module *mod;
337}; 367};
338 368
369static inline struct static_key_mod *static_key_mod(struct static_key *key)
370{
371 WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
372 return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
373}
374
375/***
376 * key->type and key->next are the same via union.
377 * This sets key->next and preserves the type bits.
378 *
379 * See additional comments above static_key_set_entries().
380 */
381static void static_key_set_mod(struct static_key *key,
382 struct static_key_mod *mod)
383{
384 unsigned long type;
385
386 WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
387 type = key->type & JUMP_TYPE_MASK;
388 key->next = mod;
389 key->type |= type;
390}
391
339static int __jump_label_mod_text_reserved(void *start, void *end) 392static int __jump_label_mod_text_reserved(void *start, void *end)
340{ 393{
341 struct module *mod; 394 struct module *mod;
@@ -358,11 +411,23 @@ static void __jump_label_mod_update(struct static_key *key)
358{ 411{
359 struct static_key_mod *mod; 412 struct static_key_mod *mod;
360 413
361 for (mod = key->next; mod; mod = mod->next) { 414 for (mod = static_key_mod(key); mod; mod = mod->next) {
362 struct module *m = mod->mod; 415 struct jump_entry *stop;
416 struct module *m;
417
418 /*
419 * NULL if the static_key is defined in a module
420 * that does not use it
421 */
422 if (!mod->entries)
423 continue;
363 424
364 __jump_label_update(key, mod->entries, 425 m = mod->mod;
365 m->jump_entries + m->num_jump_entries); 426 if (!m)
427 stop = __stop___jump_table;
428 else
429 stop = m->jump_entries + m->num_jump_entries;
430 __jump_label_update(key, mod->entries, stop);
366 } 431 }
367} 432}
368 433
@@ -397,7 +462,7 @@ static int jump_label_add_module(struct module *mod)
397 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; 462 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
398 struct jump_entry *iter; 463 struct jump_entry *iter;
399 struct static_key *key = NULL; 464 struct static_key *key = NULL;
400 struct static_key_mod *jlm; 465 struct static_key_mod *jlm, *jlm2;
401 466
402 /* if the module doesn't have jump label entries, just return */ 467 /* if the module doesn't have jump label entries, just return */
403 if (iter_start == iter_stop) 468 if (iter_start == iter_stop)
@@ -414,20 +479,32 @@ static int jump_label_add_module(struct module *mod)
414 479
415 key = iterk; 480 key = iterk;
416 if (within_module(iter->key, mod)) { 481 if (within_module(iter->key, mod)) {
417 /* 482 static_key_set_entries(key, iter);
418 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
419 */
420 *((unsigned long *)&key->entries) += (unsigned long)iter;
421 key->next = NULL;
422 continue; 483 continue;
423 } 484 }
424 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL); 485 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
425 if (!jlm) 486 if (!jlm)
426 return -ENOMEM; 487 return -ENOMEM;
488 if (!static_key_linked(key)) {
489 jlm2 = kzalloc(sizeof(struct static_key_mod),
490 GFP_KERNEL);
491 if (!jlm2) {
492 kfree(jlm);
493 return -ENOMEM;
494 }
495 preempt_disable();
496 jlm2->mod = __module_address((unsigned long)key);
497 preempt_enable();
498 jlm2->entries = static_key_entries(key);
499 jlm2->next = NULL;
500 static_key_set_mod(key, jlm2);
501 static_key_set_linked(key);
502 }
427 jlm->mod = mod; 503 jlm->mod = mod;
428 jlm->entries = iter; 504 jlm->entries = iter;
429 jlm->next = key->next; 505 jlm->next = static_key_mod(key);
430 key->next = jlm; 506 static_key_set_mod(key, jlm);
507 static_key_set_linked(key);
431 508
432 /* Only update if we've changed from our initial state */ 509 /* Only update if we've changed from our initial state */
433 if (jump_label_type(iter) != jump_label_init_type(iter)) 510 if (jump_label_type(iter) != jump_label_init_type(iter))
@@ -454,16 +531,34 @@ static void jump_label_del_module(struct module *mod)
454 if (within_module(iter->key, mod)) 531 if (within_module(iter->key, mod))
455 continue; 532 continue;
456 533
534 /* No memory during module load */
535 if (WARN_ON(!static_key_linked(key)))
536 continue;
537
457 prev = &key->next; 538 prev = &key->next;
458 jlm = key->next; 539 jlm = static_key_mod(key);
459 540
460 while (jlm && jlm->mod != mod) { 541 while (jlm && jlm->mod != mod) {
461 prev = &jlm->next; 542 prev = &jlm->next;
462 jlm = jlm->next; 543 jlm = jlm->next;
463 } 544 }
464 545
465 if (jlm) { 546 /* No memory during module load */
547 if (WARN_ON(!jlm))
548 continue;
549
550 if (prev == &key->next)
551 static_key_set_mod(key, jlm->next);
552 else
466 *prev = jlm->next; 553 *prev = jlm->next;
554
555 kfree(jlm);
556
557 jlm = static_key_mod(key);
558 /* if only one etry is left, fold it back into the static_key */
559 if (jlm->next == NULL) {
560 static_key_set_entries(key, jlm->entries);
561 static_key_clear_linked(key);
467 kfree(jlm); 562 kfree(jlm);
468 } 563 }
469 } 564 }
@@ -492,8 +587,10 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val,
492 case MODULE_STATE_COMING: 587 case MODULE_STATE_COMING:
493 jump_label_lock(); 588 jump_label_lock();
494 ret = jump_label_add_module(mod); 589 ret = jump_label_add_module(mod);
495 if (ret) 590 if (ret) {
591 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
496 jump_label_del_module(mod); 592 jump_label_del_module(mod);
593 }
497 jump_label_unlock(); 594 jump_label_unlock();
498 break; 595 break;
499 case MODULE_STATE_GOING: 596 case MODULE_STATE_GOING:
@@ -554,11 +651,14 @@ int jump_label_text_reserved(void *start, void *end)
554static void jump_label_update(struct static_key *key) 651static void jump_label_update(struct static_key *key)
555{ 652{
556 struct jump_entry *stop = __stop___jump_table; 653 struct jump_entry *stop = __stop___jump_table;
557 struct jump_entry *entry = static_key_entries(key); 654 struct jump_entry *entry;
558#ifdef CONFIG_MODULES 655#ifdef CONFIG_MODULES
559 struct module *mod; 656 struct module *mod;
560 657
561 __jump_label_mod_update(key); 658 if (static_key_linked(key)) {
659 __jump_label_mod_update(key);
660 return;
661 }
562 662
563 preempt_disable(); 663 preempt_disable();
564 mod = __module_address((unsigned long)key); 664 mod = __module_address((unsigned long)key);
@@ -566,6 +666,7 @@ static void jump_label_update(struct static_key *key)
566 stop = mod->jump_entries + mod->num_jump_entries; 666 stop = mod->jump_entries + mod->num_jump_entries;
567 preempt_enable(); 667 preempt_enable();
568#endif 668#endif
669 entry = static_key_entries(key);
569 /* if there are no users, entry can be NULL */ 670 /* if there are no users, entry can be NULL */
570 if (entry) 671 if (entry)
571 __jump_label_update(key, entry, stop); 672 __jump_label_update(key, entry, stop);