Skip to content

Commit 3821fd3

Browse files
jbaron-lgtmrostedt
authored andcommitted
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>
1 parent 7257634 commit 3821fd3

3 files changed

Lines changed: 145 additions & 35 deletions

File tree

‎Documentation/static-keys.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ or:
155155

156156
There are a few functions and macros that architectures must implement in order
157157
to take advantage of this optimization. If there is no architecture support, we
158-
simply fall back to a traditional, load, test, and jump sequence.
158+
simply fall back to a traditional, load, test, and jump sequence. Also, the
159+
struct jump_entry table must be at least 4-byte aligned because the
160+
static_key->entry field makes use of the two least significant bits.
159161

160162
* select HAVE_ARCH_JUMP_LABEL, see: arch/x86/Kconfig
161163

‎include/linux/jump_label.h‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ extern bool static_key_initialized;
8989

9090
struct static_key {
9191
atomic_t enabled;
92-
/* Set lsb bit to 1 if branch is default true, 0 ot */
93-
struct jump_entry *entries;
94-
#ifdef CONFIG_MODULES
95-
struct static_key_mod *next;
96-
#endif
92+
/*
93+
* bit 0 => 1 if key is initially true
94+
* 0 if initially false
95+
* bit 1 => 1 if points to struct static_key_mod
96+
* 0 if points to struct jump_entry
97+
*/
98+
union {
99+
unsigned long type;
100+
struct jump_entry *entries;
101+
struct static_key_mod *next;
102+
};
97103
};
98104

99105
#else
@@ -118,9 +124,10 @@ struct module;
118124

119125
#ifdef HAVE_JUMP_LABEL
120126

121-
#define JUMP_TYPE_FALSE 0UL
122-
#define JUMP_TYPE_TRUE 1UL
123-
#define JUMP_TYPE_MASK 1UL
127+
#define JUMP_TYPE_FALSE 0UL
128+
#define JUMP_TYPE_TRUE 1UL
129+
#define JUMP_TYPE_LINKED 2UL
130+
#define JUMP_TYPE_MASK 3UL
124131

125132
static __always_inline bool static_key_false(struct static_key *key)
126133
{

‎kernel/jump_label.c‎

Lines changed: 127 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,28 @@ void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry
229229

230230
static inline struct jump_entry *static_key_entries(struct static_key *key)
231231
{
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);
233234
}
234235

235236
static inline bool static_key_type(struct static_key *key)
236237
{
237-
return (unsigned long)key->entries & JUMP_TYPE_MASK;
238+
return key->type & JUMP_TYPE_TRUE;
239+
}
240+
241+
static inline bool static_key_linked(struct static_key *key)
242+
{
243+
return key->type & JUMP_TYPE_LINKED;
244+
}
245+
246+
static inline void static_key_clear_linked(struct static_key *key)
247+
{
248+
key->type &= ~JUMP_TYPE_LINKED;
249+
}
250+
251+
static inline void static_key_set_linked(struct static_key *key)
252+
{
253+
key->type |= JUMP_TYPE_LINKED;
238254
}
239255

240256
static inline struct static_key *jump_entry_key(struct jump_entry *entry)
@@ -247,6 +263,26 @@ static bool jump_entry_branch(struct jump_entry *entry)
247263
return (unsigned long)entry->key & 1UL;
248264
}
249265

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+
*/
275+
static 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+
250286
static enum jump_label_type jump_label_type(struct jump_entry *entry)
251287
{
252288
struct static_key *key = jump_entry_key(entry);
@@ -306,13 +342,7 @@ void __init jump_label_init(void)
306342
continue;
307343

308344
key = iterk;
309-
/*
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
345+
static_key_set_entries(key, iter);
316346
}
317347
static_key_initialized = true;
318348
jump_label_unlock();
@@ -336,6 +366,29 @@ struct static_key_mod {
336366
struct module *mod;
337367
};
338368

369+
static 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+
*/
381+
static 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+
339392
static int __jump_label_mod_text_reserved(void *start, void *end)
340393
{
341394
struct module *mod;
@@ -358,11 +411,23 @@ static void __jump_label_mod_update(struct static_key *key)
358411
{
359412
struct static_key_mod *mod;
360413

361-
for (mod = key->next; mod; mod = mod->next) {
362-
struct module *m = mod->mod;
414+
for (mod = static_key_mod(key); mod; mod = mod->next) {
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;
363424

364-
__jump_label_update(key, mod->entries,
365-
m->jump_entries + m->num_jump_entries);
425+
m = mod->mod;
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);
366431
}
367432
}
368433

@@ -397,7 +462,7 @@ static int jump_label_add_module(struct module *mod)
397462
struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
398463
struct jump_entry *iter;
399464
struct static_key *key = NULL;
400-
struct static_key_mod *jlm;
465+
struct static_key_mod *jlm, *jlm2;
401466

402467
/* if the module doesn't have jump label entries, just return */
403468
if (iter_start == iter_stop)
@@ -414,20 +479,32 @@ static int jump_label_add_module(struct module *mod)
414479

415480
key = iterk;
416481
if (within_module(iter->key, mod)) {
417-
/*
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;
482+
static_key_set_entries(key, iter);
422483
continue;
423484
}
424485
jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
425486
if (!jlm)
426487
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+
}
427503
jlm->mod = mod;
428504
jlm->entries = iter;
429-
jlm->next = key->next;
430-
key->next = jlm;
505+
jlm->next = static_key_mod(key);
506+
static_key_set_mod(key, jlm);
507+
static_key_set_linked(key);
431508

432509
/* Only update if we've changed from our initial state */
433510
if (jump_label_type(iter) != jump_label_init_type(iter))
@@ -454,16 +531,34 @@ static void jump_label_del_module(struct module *mod)
454531
if (within_module(iter->key, mod))
455532
continue;
456533

534+
/* No memory during module load */
535+
if (WARN_ON(!static_key_linked(key)))
536+
continue;
537+
457538
prev = &key->next;
458-
jlm = key->next;
539+
jlm = static_key_mod(key);
459540

460541
while (jlm && jlm->mod != mod) {
461542
prev = &jlm->next;
462543
jlm = jlm->next;
463544
}
464545

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
466553
*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);
467562
kfree(jlm);
468563
}
469564
}
@@ -492,8 +587,10 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val,
492587
case MODULE_STATE_COMING:
493588
jump_label_lock();
494589
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");
496592
jump_label_del_module(mod);
593+
}
497594
jump_label_unlock();
498595
break;
499596
case MODULE_STATE_GOING:
@@ -554,18 +651,22 @@ int jump_label_text_reserved(void *start, void *end)
554651
static void jump_label_update(struct static_key *key)
555652
{
556653
struct jump_entry *stop = __stop___jump_table;
557-
struct jump_entry *entry = static_key_entries(key);
654+
struct jump_entry *entry;
558655
#ifdef CONFIG_MODULES
559656
struct module *mod;
560657

561-
__jump_label_mod_update(key);
658+
if (static_key_linked(key)) {
659+
__jump_label_mod_update(key);
660+
return;
661+
}
562662

563663
preempt_disable();
564664
mod = __module_address((unsigned long)key);
565665
if (mod)
566666
stop = mod->jump_entries + mod->num_jump_entries;
567667
preempt_enable();
568668
#endif
669+
entry = static_key_entries(key);
569670
/* if there are no users, entry can be NULL */
570671
if (entry)
571672
__jump_label_update(key, entry, stop);

0 commit comments

Comments
 (0)