Skip to content

Commit 8e57f8a

Browse files
tehcastertorvalds
authored andcommitted
mm, debug_pagealloc: don't rely on static keys too early
Commit 96a2b03 ("mm, debug_pagelloc: use static keys to enable debugging") has introduced a static key to reduce overhead when debug_pagealloc is compiled in but not enabled. It relied on the assumption that jump_label_init() is called before parse_early_param() as in start_kernel(), so when the "debug_pagealloc=on" option is parsed, it is safe to enable the static key. However, it turns out multiple architectures call parse_early_param() earlier from their setup_arch(). x86 also calls jump_label_init() even earlier, so no issue was found while testing the commit, but same is not true for e.g. ppc64 and s390 where the kernel would not boot with debug_pagealloc=on as found by our QA. To fix this without tricky changes to init code of multiple architectures, this patch partially reverts the static key conversion from 96a2b03. Init-time and non-fastpath calls (such as in arch code) of debug_pagealloc_enabled() will again test a simple bool variable. Fastpath mm code is converted to a new debug_pagealloc_enabled_static() variant that relies on the static key, which is enabled in a well-defined point in mm_init() where it's guaranteed that jump_label_init() has been called, regardless of architecture. [sfr@canb.auug.org.au: export _debug_pagealloc_enabled_early] Link: http://lkml.kernel.org/r/20200106164944.063ac07b@canb.auug.org.au Link: http://lkml.kernel.org/r/20191219130612.23171-1-vbabka@suse.cz Fixes: 96a2b03 ("mm, debug_pagelloc: use static keys to enable debugging") Signed-off-by: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Matthew Wilcox <willy@infradead.org> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Qian Cai <cai@lca.pw> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4a87e2a commit 8e57f8a

6 files changed

Lines changed: 34 additions & 32 deletions

File tree

‎include/linux/mm.h‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,13 +2658,25 @@ static inline bool want_init_on_free(void)
26582658
!page_poisoning_enabled();
26592659
}
26602660

2661-
#ifdef CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT
2662-
DECLARE_STATIC_KEY_TRUE(_debug_pagealloc_enabled);
2661+
#ifdef CONFIG_DEBUG_PAGEALLOC
2662+
extern void init_debug_pagealloc(void);
26632663
#else
2664-
DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
2664+
static inline void init_debug_pagealloc(void) {}
26652665
#endif
2666+
extern bool _debug_pagealloc_enabled_early;
2667+
DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
26662668

26672669
static inline bool debug_pagealloc_enabled(void)
2670+
{
2671+
return IS_ENABLED(CONFIG_DEBUG_PAGEALLOC) &&
2672+
_debug_pagealloc_enabled_early;
2673+
}
2674+
2675+
/*
2676+
* For use in fast paths after init_debug_pagealloc() has run, or when a
2677+
* false negative result is not harmful when called too early.
2678+
*/
2679+
static inline bool debug_pagealloc_enabled_static(void)
26682680
{
26692681
if (!IS_ENABLED(CONFIG_DEBUG_PAGEALLOC))
26702682
return false;

‎init/main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ static void __init mm_init(void)
553553
* bigger than MAX_ORDER unless SPARSEMEM.
554554
*/
555555
page_ext_init_flatmem();
556+
init_debug_pagealloc();
556557
report_meminit();
557558
mem_init();
558559
kmem_cache_init();

‎mm/page_alloc.c‎

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -694,34 +694,27 @@ void prep_compound_page(struct page *page, unsigned int order)
694694
#ifdef CONFIG_DEBUG_PAGEALLOC
695695
unsigned int _debug_guardpage_minorder;
696696

697-
#ifdef CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT
698-
DEFINE_STATIC_KEY_TRUE(_debug_pagealloc_enabled);
699-
#else
697+
bool _debug_pagealloc_enabled_early __read_mostly
698+
= IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
699+
EXPORT_SYMBOL(_debug_pagealloc_enabled_early);
700700
DEFINE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
701-
#endif
702701
EXPORT_SYMBOL(_debug_pagealloc_enabled);
703702

704703
DEFINE_STATIC_KEY_FALSE(_debug_guardpage_enabled);
705704

706705
static int __init early_debug_pagealloc(char *buf)
707706
{
708-
bool enable = false;
709-
710-
if (kstrtobool(buf, &enable))
711-
return -EINVAL;
712-
713-
if (enable)
714-
static_branch_enable(&_debug_pagealloc_enabled);
715-
716-
return 0;
707+
return kstrtobool(buf, &_debug_pagealloc_enabled_early);
717708
}
718709
early_param("debug_pagealloc", early_debug_pagealloc);
719710

720-
static void init_debug_guardpage(void)
711+
void init_debug_pagealloc(void)
721712
{
722713
if (!debug_pagealloc_enabled())
723714
return;
724715

716+
static_branch_enable(&_debug_pagealloc_enabled);
717+
725718
if (!debug_guardpage_minorder())
726719
return;
727720

@@ -1186,7 +1179,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
11861179
*/
11871180
arch_free_page(page, order);
11881181

1189-
if (debug_pagealloc_enabled())
1182+
if (debug_pagealloc_enabled_static())
11901183
kernel_map_pages(page, 1 << order, 0);
11911184

11921185
kasan_free_nondeferred_pages(page, order);
@@ -1207,7 +1200,7 @@ static bool free_pcp_prepare(struct page *page)
12071200

12081201
static bool bulkfree_pcp_prepare(struct page *page)
12091202
{
1210-
if (debug_pagealloc_enabled())
1203+
if (debug_pagealloc_enabled_static())
12111204
return free_pages_check(page);
12121205
else
12131206
return false;
@@ -1221,7 +1214,7 @@ static bool bulkfree_pcp_prepare(struct page *page)
12211214
*/
12221215
static bool free_pcp_prepare(struct page *page)
12231216
{
1224-
if (debug_pagealloc_enabled())
1217+
if (debug_pagealloc_enabled_static())
12251218
return free_pages_prepare(page, 0, true);
12261219
else
12271220
return free_pages_prepare(page, 0, false);
@@ -1973,10 +1966,6 @@ void __init page_alloc_init_late(void)
19731966

19741967
for_each_populated_zone(zone)
19751968
set_zone_contiguous(zone);
1976-
1977-
#ifdef CONFIG_DEBUG_PAGEALLOC
1978-
init_debug_guardpage();
1979-
#endif
19801969
}
19811970

19821971
#ifdef CONFIG_CMA
@@ -2106,7 +2095,7 @@ static inline bool free_pages_prezeroed(void)
21062095
*/
21072096
static inline bool check_pcp_refill(struct page *page)
21082097
{
2109-
if (debug_pagealloc_enabled())
2098+
if (debug_pagealloc_enabled_static())
21102099
return check_new_page(page);
21112100
else
21122101
return false;
@@ -2128,7 +2117,7 @@ static inline bool check_pcp_refill(struct page *page)
21282117
}
21292118
static inline bool check_new_pcp(struct page *page)
21302119
{
2131-
if (debug_pagealloc_enabled())
2120+
if (debug_pagealloc_enabled_static())
21322121
return check_new_page(page);
21332122
else
21342123
return false;
@@ -2155,7 +2144,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
21552144
set_page_refcounted(page);
21562145

21572146
arch_alloc_page(page, order);
2158-
if (debug_pagealloc_enabled())
2147+
if (debug_pagealloc_enabled_static())
21592148
kernel_map_pages(page, 1 << order, 1);
21602149
kasan_alloc_pages(page, order);
21612150
kernel_poison_pages(page, 1 << order, 1);

‎mm/slab.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ static void kmem_rcu_free(struct rcu_head *head)
14161416
#if DEBUG
14171417
static bool is_debug_pagealloc_cache(struct kmem_cache *cachep)
14181418
{
1419-
if (debug_pagealloc_enabled() && OFF_SLAB(cachep) &&
1419+
if (debug_pagealloc_enabled_static() && OFF_SLAB(cachep) &&
14201420
(cachep->size % PAGE_SIZE) == 0)
14211421
return true;
14221422

@@ -2008,7 +2008,7 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags)
20082008
* to check size >= 256. It guarantees that all necessary small
20092009
* sized slab is initialized in current slab initialization sequence.
20102010
*/
2011-
if (debug_pagealloc_enabled() && (flags & SLAB_POISON) &&
2011+
if (debug_pagealloc_enabled_static() && (flags & SLAB_POISON) &&
20122012
size >= 256 && cachep->object_size > cache_line_size()) {
20132013
if (size < PAGE_SIZE || size % PAGE_SIZE == 0) {
20142014
size_t tmp_size = ALIGN(size, PAGE_SIZE);

‎mm/slub.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
288288
unsigned long freepointer_addr;
289289
void *p;
290290

291-
if (!debug_pagealloc_enabled())
291+
if (!debug_pagealloc_enabled_static())
292292
return get_freepointer(s, object);
293293

294294
freepointer_addr = (unsigned long)object + s->offset;

‎mm/vmalloc.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ static void free_unmap_vmap_area(struct vmap_area *va)
13831383
{
13841384
flush_cache_vunmap(va->va_start, va->va_end);
13851385
unmap_vmap_area(va);
1386-
if (debug_pagealloc_enabled())
1386+
if (debug_pagealloc_enabled_static())
13871387
flush_tlb_kernel_range(va->va_start, va->va_end);
13881388

13891389
free_vmap_area_noflush(va);
@@ -1681,7 +1681,7 @@ static void vb_free(const void *addr, unsigned long size)
16811681

16821682
vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
16831683

1684-
if (debug_pagealloc_enabled())
1684+
if (debug_pagealloc_enabled_static())
16851685
flush_tlb_kernel_range((unsigned long)addr,
16861686
(unsigned long)addr + size);
16871687

0 commit comments

Comments
 (0)