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