unsigned long address, unsigned long size,
                                   pte_fn_t fn, void *data);
 
+extern void init_mem_debugging_and_hardening(void);
 #ifdef CONFIG_PAGE_POISONING
 extern bool page_poisoning_enabled(void);
 extern void kernel_poison_pages(struct page *page, int numpages, int enable);
                                        int enable) { }
 #endif
 
-#ifdef CONFIG_INIT_ON_ALLOC_DEFAULT_ON
-DECLARE_STATIC_KEY_TRUE(init_on_alloc);
-#else
 DECLARE_STATIC_KEY_FALSE(init_on_alloc);
-#endif
 static inline bool want_init_on_alloc(gfp_t flags)
 {
-       if (static_branch_unlikely(&init_on_alloc) &&
-           !page_poisoning_enabled())
+       if (static_branch_unlikely(&init_on_alloc))
                return true;
        return flags & __GFP_ZERO;
 }
 
-#ifdef CONFIG_INIT_ON_FREE_DEFAULT_ON
-DECLARE_STATIC_KEY_TRUE(init_on_free);
-#else
 DECLARE_STATIC_KEY_FALSE(init_on_free);
-#endif
 static inline bool want_init_on_free(void)
 {
-       return static_branch_unlikely(&init_on_free) &&
-              !page_poisoning_enabled();
+       return static_branch_unlikely(&init_on_free);
 }
 
-#ifdef CONFIG_DEBUG_PAGEALLOC
-extern void init_debug_pagealloc(void);
-#else
-static inline void init_debug_pagealloc(void) {}
-#endif
 extern bool _debug_pagealloc_enabled_early;
 DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
 
 
 
 int percpu_pagelist_fraction;
 gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
-#ifdef CONFIG_INIT_ON_ALLOC_DEFAULT_ON
-DEFINE_STATIC_KEY_TRUE(init_on_alloc);
-#else
 DEFINE_STATIC_KEY_FALSE(init_on_alloc);
-#endif
 EXPORT_SYMBOL(init_on_alloc);
 
-#ifdef CONFIG_INIT_ON_FREE_DEFAULT_ON
-DEFINE_STATIC_KEY_TRUE(init_on_free);
-#else
 DEFINE_STATIC_KEY_FALSE(init_on_free);
-#endif
 EXPORT_SYMBOL(init_on_free);
 
+static bool _init_on_alloc_enabled_early __read_mostly
+                               = IS_ENABLED(CONFIG_INIT_ON_ALLOC_DEFAULT_ON);
 static int __init early_init_on_alloc(char *buf)
 {
-       int ret;
-       bool bool_result;
 
-       ret = kstrtobool(buf, &bool_result);
-       if (ret)
-               return ret;
-       if (bool_result && page_poisoning_enabled())
-               pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, will take precedence over init_on_alloc\n");
-       if (bool_result)
-               static_branch_enable(&init_on_alloc);
-       else
-               static_branch_disable(&init_on_alloc);
-       return 0;
+       return kstrtobool(buf, &_init_on_alloc_enabled_early);
 }
 early_param("init_on_alloc", early_init_on_alloc);
 
+static bool _init_on_free_enabled_early __read_mostly
+                               = IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON);
 static int __init early_init_on_free(char *buf)
 {
-       int ret;
-       bool bool_result;
-
-       ret = kstrtobool(buf, &bool_result);
-       if (ret)
-               return ret;
-       if (bool_result && page_poisoning_enabled())
-               pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, will take precedence over init_on_free\n");
-       if (bool_result)
-               static_branch_enable(&init_on_free);
-       else
-               static_branch_disable(&init_on_free);
-       return 0;
+       return kstrtobool(buf, &_init_on_free_enabled_early);
 }
 early_param("init_on_free", early_init_on_free);
 
 }
 early_param("debug_pagealloc", early_debug_pagealloc);
 
-void init_debug_pagealloc(void)
-{
-       if (!debug_pagealloc_enabled())
-               return;
-
-       static_branch_enable(&_debug_pagealloc_enabled);
-
-       if (!debug_guardpage_minorder())
-               return;
-
-       static_branch_enable(&_debug_guardpage_enabled);
-}
-
 static int __init debug_guardpage_minorder_setup(char *buf)
 {
        unsigned long res;
                                unsigned int order, int migratetype) {}
 #endif
 
+/*
+ * Enable static keys related to various memory debugging and hardening options.
+ * Some override others, and depend on early params that are evaluated in the
+ * order of appearance. So we need to first gather the full picture of what was
+ * enabled, and then make decisions.
+ */
+void init_mem_debugging_and_hardening(void)
+{
+       if (_init_on_alloc_enabled_early) {
+               if (page_poisoning_enabled())
+                       pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, "
+                               "will take precedence over init_on_alloc\n");
+               else
+                       static_branch_enable(&init_on_alloc);
+       }
+       if (_init_on_free_enabled_early) {
+               if (page_poisoning_enabled())
+                       pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, "
+                               "will take precedence over init_on_free\n");
+               else
+                       static_branch_enable(&init_on_free);
+       }
+
+#ifdef CONFIG_DEBUG_PAGEALLOC
+       if (!debug_pagealloc_enabled())
+               return;
+
+       static_branch_enable(&_debug_pagealloc_enabled);
+
+       if (!debug_guardpage_minorder())
+               return;
+
+       static_branch_enable(&_debug_guardpage_enabled);
+#endif
+}
+
 static inline void set_buddy_order(struct page *page, unsigned int order)
 {
        set_page_private(page, order);