Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/kernel.h>
0003 #include <linux/string.h>
0004 #include <linux/mm.h>
0005 #include <linux/mmdebug.h>
0006 #include <linux/highmem.h>
0007 #include <linux/page_ext.h>
0008 #include <linux/poison.h>
0009 #include <linux/ratelimit.h>
0010 #include <linux/kasan.h>
0011 
0012 bool _page_poisoning_enabled_early;
0013 EXPORT_SYMBOL(_page_poisoning_enabled_early);
0014 DEFINE_STATIC_KEY_FALSE(_page_poisoning_enabled);
0015 EXPORT_SYMBOL(_page_poisoning_enabled);
0016 
0017 static int __init early_page_poison_param(char *buf)
0018 {
0019     return kstrtobool(buf, &_page_poisoning_enabled_early);
0020 }
0021 early_param("page_poison", early_page_poison_param);
0022 
0023 static void poison_page(struct page *page)
0024 {
0025     void *addr = kmap_atomic(page);
0026 
0027     /* KASAN still think the page is in-use, so skip it. */
0028     kasan_disable_current();
0029     memset(kasan_reset_tag(addr), PAGE_POISON, PAGE_SIZE);
0030     kasan_enable_current();
0031     kunmap_atomic(addr);
0032 }
0033 
0034 void __kernel_poison_pages(struct page *page, int n)
0035 {
0036     int i;
0037 
0038     for (i = 0; i < n; i++)
0039         poison_page(page + i);
0040 }
0041 
0042 static bool single_bit_flip(unsigned char a, unsigned char b)
0043 {
0044     unsigned char error = a ^ b;
0045 
0046     return error && !(error & (error - 1));
0047 }
0048 
0049 static void check_poison_mem(struct page *page, unsigned char *mem, size_t bytes)
0050 {
0051     static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
0052     unsigned char *start;
0053     unsigned char *end;
0054 
0055     start = memchr_inv(mem, PAGE_POISON, bytes);
0056     if (!start)
0057         return;
0058 
0059     for (end = mem + bytes - 1; end > start; end--) {
0060         if (*end != PAGE_POISON)
0061             break;
0062     }
0063 
0064     if (!__ratelimit(&ratelimit))
0065         return;
0066     else if (start == end && single_bit_flip(*start, PAGE_POISON))
0067         pr_err("pagealloc: single bit error\n");
0068     else
0069         pr_err("pagealloc: memory corruption\n");
0070 
0071     print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
0072             end - start + 1, 1);
0073     dump_stack();
0074     dump_page(page, "pagealloc: corrupted page details");
0075 }
0076 
0077 static void unpoison_page(struct page *page)
0078 {
0079     void *addr;
0080 
0081     addr = kmap_atomic(page);
0082     kasan_disable_current();
0083     /*
0084      * Page poisoning when enabled poisons each and every page
0085      * that is freed to buddy. Thus no extra check is done to
0086      * see if a page was poisoned.
0087      */
0088     check_poison_mem(page, kasan_reset_tag(addr), PAGE_SIZE);
0089     kasan_enable_current();
0090     kunmap_atomic(addr);
0091 }
0092 
0093 void __kernel_unpoison_pages(struct page *page, int n)
0094 {
0095     int i;
0096 
0097     for (i = 0; i < n; i++)
0098         unpoison_page(page + i);
0099 }
0100 
0101 #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
0102 void __kernel_map_pages(struct page *page, int numpages, int enable)
0103 {
0104     /* This function does nothing, all work is done via poison pages */
0105 }
0106 #endif