blob: 5136e6818da8dd1d0c9138c39be197e2ac183c52 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
He Zheb1e3a252018-08-14 23:33:43 +08002
3#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4
Paul Gortmaker13fe86f42015-08-24 19:34:55 -04005#include <linux/init.h>
Arjan van de Ven6784f7d2008-10-05 11:33:42 -07006#include <linux/sched.h>
Arjan van de Ven304e6292008-10-05 12:09:03 -07007#include <linux/kthread.h>
8#include <linux/workqueue.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07009#include <linux/memblock.h>
10
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070011#include <asm/proto.h>
Yi Wang89f579c2018-11-22 10:04:09 +080012#include <asm/setup.h>
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070013
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 Ven6784f7d2008-10-05 11:33:42 -070020#define MAX_SCAN_AREAS 8
21
22static int __read_mostly memory_corruption_check = -1;
23
24static unsigned __read_mostly corruption_check_size = 64*1024;
25static unsigned __read_mostly corruption_check_period = 60; /* seconds */
26
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070027static struct scan_area {
28 u64 addr;
29 u64 size;
30} scan_areas[MAX_SCAN_AREAS];
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070031static int num_scan_areas;
32
Arjan van de Venb43d1962008-10-05 12:21:32 -070033static __init int set_corruption_check(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070034{
Shuah Khan5abe68e2012-05-06 11:55:08 -060035 ssize_t ret;
36 unsigned long val;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070037
He Zheccde4602018-08-14 23:33:42 +080038 if (!arg) {
39 pr_err("memory_corruption_check config string not provided\n");
40 return -EINVAL;
41 }
42
Shuah Khan5abe68e2012-05-06 11:55:08 -060043 ret = kstrtoul(arg, 10, &val);
44 if (ret)
45 return ret;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070046
Shuah Khan5abe68e2012-05-06 11:55:08 -060047 memory_corruption_check = val;
He Zheb1e3a252018-08-14 23:33:43 +080048
Shuah Khan5abe68e2012-05-06 11:55:08 -060049 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070050}
51early_param("memory_corruption_check", set_corruption_check);
52
Arjan van de Venb43d1962008-10-05 12:21:32 -070053static __init int set_corruption_check_period(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070054{
Shuah Khan5abe68e2012-05-06 11:55:08 -060055 ssize_t ret;
56 unsigned long val;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070057
He Zheccde4602018-08-14 23:33:42 +080058 if (!arg) {
59 pr_err("memory_corruption_check_period config string not provided\n");
60 return -EINVAL;
61 }
62
Shuah Khan5abe68e2012-05-06 11:55:08 -060063 ret = kstrtoul(arg, 10, &val);
64 if (ret)
65 return ret;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070066
Shuah Khan5abe68e2012-05-06 11:55:08 -060067 corruption_check_period = val;
68 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070069}
70early_param("memory_corruption_check_period", set_corruption_check_period);
71
Arjan van de Venb43d1962008-10-05 12:21:32 -070072static __init int set_corruption_check_size(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070073{
74 char *end;
75 unsigned size;
76
He Zheccde4602018-08-14 23:33:42 +080077 if (!arg) {
78 pr_err("memory_corruption_check_size config string not provided\n");
79 return -EINVAL;
80 }
81
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070082 size = memparse(arg, &end);
83
84 if (*end == '\0')
85 corruption_check_size = size;
86
87 return (size == corruption_check_size) ? 0 : -EINVAL;
88}
89early_param("memory_corruption_check_size", set_corruption_check_size);
90
91
92void __init setup_bios_corruption_check(void)
93{
Tejun Heo8d89ac82011-07-12 11:16:00 +020094 phys_addr_t start, end;
95 u64 i;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070096
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 Luckfc6daaf2015-06-24 16:58:09 -0700115 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
116 NULL) {
Tejun Heo8d89ac82011-07-12 11:16:00 +0200117 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 Ven6784f7d2008-10-05 11:33:42 -0700123
Tejun Heo24aa0782011-07-12 11:16:06 +0200124 memblock_reserve(start, end - start);
Tejun Heo8d89ac82011-07-12 11:16:00 +0200125 scan_areas[num_scan_areas].addr = start;
126 scan_areas[num_scan_areas].size = end - start;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700127
128 /* Assume we've already mapped this early memory */
Tejun Heo8d89ac82011-07-12 11:16:00 +0200129 memset(__va(start), 0, end - start);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700130
Tejun Heo8d89ac82011-07-12 11:16:00 +0200131 if (++num_scan_areas >= MAX_SCAN_AREAS)
132 break;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700133 }
134
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000135 if (num_scan_areas)
He Zheb1e3a252018-08-14 23:33:43 +0800136 pr_info("Scanning %d areas for low memory corruption\n", num_scan_areas);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700137}
138
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700139
Yi Wang89f579c2018-11-22 10:04:09 +0800140static void check_for_bios_corruption(void)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700141{
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 Zheb1e3a252018-08-14 23:33:43 +0800155 pr_err("Corrupted low memory at %p (%lx phys) = %08lx\n", addr, __pa(addr), *addr);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700156 corruption = 1;
157 *addr = 0;
158 }
159 }
160
Arjan van de Venb43d1962008-10-05 12:21:32 -0700161 WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700162}
163
Arjan van de Ven304e6292008-10-05 12:09:03 -0700164static void check_corruption(struct work_struct *dummy);
165static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
166
167static void check_corruption(struct work_struct *dummy)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700168{
169 check_for_bios_corruption();
Arjan van de Ven304e6292008-10-05 12:09:03 -0700170 schedule_delayed_work(&bios_check_work,
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000171 round_jiffies_relative(corruption_check_period*HZ));
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700172}
173
Arjan van de Ven304e6292008-10-05 12:09:03 -0700174static int start_periodic_check_for_corruption(void)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700175{
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000176 if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
Arjan van de Ven304e6292008-10-05 12:09:03 -0700177 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700178
He Zheb1e3a252018-08-14 23:33:43 +0800179 pr_info("Scanning for low memory corruption every %d seconds\n", corruption_check_period);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700180
Arjan van de Ven304e6292008-10-05 12:09:03 -0700181 /* First time we run the checks right away */
182 schedule_delayed_work(&bios_check_work, 0);
He Zheb1e3a252018-08-14 23:33:43 +0800183
Arjan van de Ven304e6292008-10-05 12:09:03 -0700184 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700185}
Paul Gortmaker13fe86f42015-08-24 19:34:55 -0400186device_initcall(start_periodic_check_for_corruption);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700187