1*884c175fSKefeng Wang // SPDX-License-Identifier: GPL-2.0
2*884c175fSKefeng Wang #include <linux/mm.h>
3*884c175fSKefeng Wang #include <linux/page-isolation.h>
4*884c175fSKefeng Wang
5*884c175fSKefeng Wang unsigned int _debug_guardpage_minorder;
6*884c175fSKefeng Wang
7*884c175fSKefeng Wang bool _debug_pagealloc_enabled_early __read_mostly
8*884c175fSKefeng Wang = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
9*884c175fSKefeng Wang EXPORT_SYMBOL(_debug_pagealloc_enabled_early);
10*884c175fSKefeng Wang DEFINE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
11*884c175fSKefeng Wang EXPORT_SYMBOL(_debug_pagealloc_enabled);
12*884c175fSKefeng Wang
13*884c175fSKefeng Wang DEFINE_STATIC_KEY_FALSE(_debug_guardpage_enabled);
14*884c175fSKefeng Wang
early_debug_pagealloc(char * buf)15*884c175fSKefeng Wang static int __init early_debug_pagealloc(char *buf)
16*884c175fSKefeng Wang {
17*884c175fSKefeng Wang return kstrtobool(buf, &_debug_pagealloc_enabled_early);
18*884c175fSKefeng Wang }
19*884c175fSKefeng Wang early_param("debug_pagealloc", early_debug_pagealloc);
20*884c175fSKefeng Wang
debug_guardpage_minorder_setup(char * buf)21*884c175fSKefeng Wang static int __init debug_guardpage_minorder_setup(char *buf)
22*884c175fSKefeng Wang {
23*884c175fSKefeng Wang unsigned long res;
24*884c175fSKefeng Wang
25*884c175fSKefeng Wang if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
26*884c175fSKefeng Wang pr_err("Bad debug_guardpage_minorder value\n");
27*884c175fSKefeng Wang return 0;
28*884c175fSKefeng Wang }
29*884c175fSKefeng Wang _debug_guardpage_minorder = res;
30*884c175fSKefeng Wang pr_info("Setting debug_guardpage_minorder to %lu\n", res);
31*884c175fSKefeng Wang return 0;
32*884c175fSKefeng Wang }
33*884c175fSKefeng Wang early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup);
34*884c175fSKefeng Wang
__set_page_guard(struct zone * zone,struct page * page,unsigned int order,int migratetype)35*884c175fSKefeng Wang bool __set_page_guard(struct zone *zone, struct page *page, unsigned int order,
36*884c175fSKefeng Wang int migratetype)
37*884c175fSKefeng Wang {
38*884c175fSKefeng Wang if (order >= debug_guardpage_minorder())
39*884c175fSKefeng Wang return false;
40*884c175fSKefeng Wang
41*884c175fSKefeng Wang __SetPageGuard(page);
42*884c175fSKefeng Wang INIT_LIST_HEAD(&page->buddy_list);
43*884c175fSKefeng Wang set_page_private(page, order);
44*884c175fSKefeng Wang /* Guard pages are not available for any usage */
45*884c175fSKefeng Wang if (!is_migrate_isolate(migratetype))
46*884c175fSKefeng Wang __mod_zone_freepage_state(zone, -(1 << order), migratetype);
47*884c175fSKefeng Wang
48*884c175fSKefeng Wang return true;
49*884c175fSKefeng Wang }
50*884c175fSKefeng Wang
__clear_page_guard(struct zone * zone,struct page * page,unsigned int order,int migratetype)51*884c175fSKefeng Wang void __clear_page_guard(struct zone *zone, struct page *page, unsigned int order,
52*884c175fSKefeng Wang int migratetype)
53*884c175fSKefeng Wang {
54*884c175fSKefeng Wang __ClearPageGuard(page);
55*884c175fSKefeng Wang
56*884c175fSKefeng Wang set_page_private(page, 0);
57*884c175fSKefeng Wang if (!is_migrate_isolate(migratetype))
58*884c175fSKefeng Wang __mod_zone_freepage_state(zone, (1 << order), migratetype);
59*884c175fSKefeng Wang }
60