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