Skip to content

Commit 51cba1e

Browse files
keesKAGA-KOKO
authored andcommitted
init_on_alloc: Optimize static branches
The state of CONFIG_INIT_ON_ALLOC_DEFAULT_ON (and ...ON_FREE...) did not change the assembly ordering of the static branches: they were always out of line. Use the new jump_label macros to check the CONFIG settings to default to the "expected" state, which slightly optimizes the resulting assembly code. Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Alexander Potapenko <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0d66ccc commit 51cba1e

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

include/linux/mm.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,18 +2904,20 @@ static inline void kernel_poison_pages(struct page *page, int numpages) { }
29042904
static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
29052905
#endif
29062906

2907-
DECLARE_STATIC_KEY_FALSE(init_on_alloc);
2907+
DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc);
29082908
static inline bool want_init_on_alloc(gfp_t flags)
29092909
{
2910-
if (static_branch_unlikely(&init_on_alloc))
2910+
if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
2911+
&init_on_alloc))
29112912
return true;
29122913
return flags & __GFP_ZERO;
29132914
}
29142915

2915-
DECLARE_STATIC_KEY_FALSE(init_on_free);
2916+
DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free);
29162917
static inline bool want_init_on_free(void)
29172918
{
2918-
return static_branch_unlikely(&init_on_free);
2919+
return static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
2920+
&init_on_free);
29192921
}
29202922

29212923
extern bool _debug_pagealloc_enabled_early;

mm/page_alloc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ unsigned long totalcma_pages __read_mostly;
167167

168168
int percpu_pagelist_fraction;
169169
gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
170-
DEFINE_STATIC_KEY_FALSE(init_on_alloc);
170+
DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc);
171171
EXPORT_SYMBOL(init_on_alloc);
172172

173-
DEFINE_STATIC_KEY_FALSE(init_on_free);
173+
DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free);
174174
EXPORT_SYMBOL(init_on_free);
175175

176176
static bool _init_on_alloc_enabled_early __read_mostly

mm/slab.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
601601

602602
static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
603603
{
604-
if (static_branch_unlikely(&init_on_alloc)) {
604+
if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
605+
&init_on_alloc)) {
605606
if (c->ctor)
606607
return false;
607608
if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
@@ -613,7 +614,8 @@ static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
613614

614615
static inline bool slab_want_init_on_free(struct kmem_cache *c)
615616
{
616-
if (static_branch_unlikely(&init_on_free))
617+
if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
618+
&init_on_free))
617619
return !(c->ctor ||
618620
(c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
619621
return false;

0 commit comments

Comments
 (0)