1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/init.h> 3 #include <linux/sched.h> 4 #include <linux/kthread.h> 5 #include <linux/workqueue.h> 6 #include <linux/memblock.h> 7 8 #include <asm/proto.h> 9 10 /* 11 * Some BIOSes seem to corrupt the low 64k of memory during events 12 * like suspend/resume and unplugging an HDMI cable. Reserve all 13 * remaining free memory in that area and fill it with a distinct 14 * pattern. 15 */ 16 #define MAX_SCAN_AREAS 8 17 18 static int __read_mostly memory_corruption_check = -1; 19 20 static unsigned __read_mostly corruption_check_size = 64*1024; 21 static unsigned __read_mostly corruption_check_period = 60; /* seconds */ 22 23 static struct scan_area { 24 u64 addr; 25 u64 size; 26 } scan_areas[MAX_SCAN_AREAS]; 27 static int num_scan_areas; 28 29 static __init int set_corruption_check(char *arg) 30 { 31 ssize_t ret; 32 unsigned long val; 33 34 if (!arg) { 35 pr_err("memory_corruption_check config string not provided\n"); 36 return -EINVAL; 37 } 38 39 ret = kstrtoul(arg, 10, &val); 40 if (ret) 41 return ret; 42 43 memory_corruption_check = val; 44 return 0; 45 } 46 early_param("memory_corruption_check", set_corruption_check); 47 48 static __init int set_corruption_check_period(char *arg) 49 { 50 ssize_t ret; 51 unsigned long val; 52 53 if (!arg) { 54 pr_err("memory_corruption_check_period config string not provided\n"); 55 return -EINVAL; 56 } 57 58 ret = kstrtoul(arg, 10, &val); 59 if (ret) 60 return ret; 61 62 corruption_check_period = val; 63 return 0; 64 } 65 early_param("memory_corruption_check_period", set_corruption_check_period); 66 67 static __init int set_corruption_check_size(char *arg) 68 { 69 char *end; 70 unsigned size; 71 72 if (!arg) { 73 pr_err("memory_corruption_check_size config string not provided\n"); 74 return -EINVAL; 75 } 76 77 size = memparse(arg, &end); 78 79 if (*end == '\0') 80 corruption_check_size = size; 81 82 return (size == corruption_check_size) ? 0 : -EINVAL; 83 } 84 early_param("memory_corruption_check_size", set_corruption_check_size); 85 86 87 void __init setup_bios_corruption_check(void) 88 { 89 phys_addr_t start, end; 90 u64 i; 91 92 if (memory_corruption_check == -1) { 93 memory_corruption_check = 94 #ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK 95 1 96 #else 97 0 98 #endif 99 ; 100 } 101 102 if (corruption_check_size == 0) 103 memory_corruption_check = 0; 104 105 if (!memory_corruption_check) 106 return; 107 108 corruption_check_size = round_up(corruption_check_size, PAGE_SIZE); 109 110 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, 111 NULL) { 112 start = clamp_t(phys_addr_t, round_up(start, PAGE_SIZE), 113 PAGE_SIZE, corruption_check_size); 114 end = clamp_t(phys_addr_t, round_down(end, PAGE_SIZE), 115 PAGE_SIZE, corruption_check_size); 116 if (start >= end) 117 continue; 118 119 memblock_reserve(start, end - start); 120 scan_areas[num_scan_areas].addr = start; 121 scan_areas[num_scan_areas].size = end - start; 122 123 /* Assume we've already mapped this early memory */ 124 memset(__va(start), 0, end - start); 125 126 if (++num_scan_areas >= MAX_SCAN_AREAS) 127 break; 128 } 129 130 if (num_scan_areas) 131 printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas); 132 } 133 134 135 void check_for_bios_corruption(void) 136 { 137 int i; 138 int corruption = 0; 139 140 if (!memory_corruption_check) 141 return; 142 143 for (i = 0; i < num_scan_areas; i++) { 144 unsigned long *addr = __va(scan_areas[i].addr); 145 unsigned long size = scan_areas[i].size; 146 147 for (; size; addr++, size -= sizeof(unsigned long)) { 148 if (!*addr) 149 continue; 150 printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n", 151 addr, __pa(addr), *addr); 152 corruption = 1; 153 *addr = 0; 154 } 155 } 156 157 WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n"); 158 } 159 160 static void check_corruption(struct work_struct *dummy); 161 static DECLARE_DELAYED_WORK(bios_check_work, check_corruption); 162 163 static void check_corruption(struct work_struct *dummy) 164 { 165 check_for_bios_corruption(); 166 schedule_delayed_work(&bios_check_work, 167 round_jiffies_relative(corruption_check_period*HZ)); 168 } 169 170 static int start_periodic_check_for_corruption(void) 171 { 172 if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0) 173 return 0; 174 175 printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n", 176 corruption_check_period); 177 178 /* First time we run the checks right away */ 179 schedule_delayed_work(&bios_check_work, 0); 180 return 0; 181 } 182 device_initcall(start_periodic_check_for_corruption); 183 184