]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/check.c
Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / check.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
13fe86f4 2#include <linux/init.h>
6784f7d0 3#include <linux/sched.h>
304e629b
AV
4#include <linux/kthread.h>
5#include <linux/workqueue.h>
72d7c3b3
YL
6#include <linux/memblock.h>
7
6784f7d0
AV
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 */
6784f7d0
AV
16#define MAX_SCAN_AREAS 8
17
18static int __read_mostly memory_corruption_check = -1;
19
20static unsigned __read_mostly corruption_check_size = 64*1024;
21static unsigned __read_mostly corruption_check_period = 60; /* seconds */
22
72d7c3b3
YL
23static struct scan_area {
24 u64 addr;
25 u64 size;
26} scan_areas[MAX_SCAN_AREAS];
6784f7d0
AV
27static int num_scan_areas;
28
b43d196c 29static __init int set_corruption_check(char *arg)
6784f7d0 30{
5abe68e4
SK
31 ssize_t ret;
32 unsigned long val;
6784f7d0 33
5abe68e4
SK
34 ret = kstrtoul(arg, 10, &val);
35 if (ret)
36 return ret;
6784f7d0 37
5abe68e4
SK
38 memory_corruption_check = val;
39 return 0;
6784f7d0
AV
40}
41early_param("memory_corruption_check", set_corruption_check);
42
b43d196c 43static __init int set_corruption_check_period(char *arg)
6784f7d0 44{
5abe68e4
SK
45 ssize_t ret;
46 unsigned long val;
6784f7d0 47
5abe68e4
SK
48 ret = kstrtoul(arg, 10, &val);
49 if (ret)
50 return ret;
6784f7d0 51
5abe68e4
SK
52 corruption_check_period = val;
53 return 0;
6784f7d0
AV
54}
55early_param("memory_corruption_check_period", set_corruption_check_period);
56
b43d196c 57static __init int set_corruption_check_size(char *arg)
6784f7d0
AV
58{
59 char *end;
60 unsigned size;
61
62 size = memparse(arg, &end);
63
64 if (*end == '\0')
65 corruption_check_size = size;
66
67 return (size == corruption_check_size) ? 0 : -EINVAL;
68}
69early_param("memory_corruption_check_size", set_corruption_check_size);
70
71
72void __init setup_bios_corruption_check(void)
73{
8d89ac80
TH
74 phys_addr_t start, end;
75 u64 i;
6784f7d0
AV
76
77 if (memory_corruption_check == -1) {
78 memory_corruption_check =
79#ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
80 1
81#else
82 0
83#endif
84 ;
85 }
86
87 if (corruption_check_size == 0)
88 memory_corruption_check = 0;
89
90 if (!memory_corruption_check)
91 return;
92
93 corruption_check_size = round_up(corruption_check_size, PAGE_SIZE);
94
fc6daaf9
TL
95 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
96 NULL) {
8d89ac80
TH
97 start = clamp_t(phys_addr_t, round_up(start, PAGE_SIZE),
98 PAGE_SIZE, corruption_check_size);
99 end = clamp_t(phys_addr_t, round_down(end, PAGE_SIZE),
100 PAGE_SIZE, corruption_check_size);
101 if (start >= end)
102 continue;
6784f7d0 103
24aa0788 104 memblock_reserve(start, end - start);
8d89ac80
TH
105 scan_areas[num_scan_areas].addr = start;
106 scan_areas[num_scan_areas].size = end - start;
6784f7d0
AV
107
108 /* Assume we've already mapped this early memory */
8d89ac80 109 memset(__va(start), 0, end - start);
6784f7d0 110
8d89ac80
TH
111 if (++num_scan_areas >= MAX_SCAN_AREAS)
112 break;
6784f7d0
AV
113 }
114
a7bd1daf
NC
115 if (num_scan_areas)
116 printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas);
6784f7d0
AV
117}
118
6784f7d0
AV
119
120void check_for_bios_corruption(void)
121{
122 int i;
123 int corruption = 0;
124
125 if (!memory_corruption_check)
126 return;
127
128 for (i = 0; i < num_scan_areas; i++) {
129 unsigned long *addr = __va(scan_areas[i].addr);
130 unsigned long size = scan_areas[i].size;
131
132 for (; size; addr++, size -= sizeof(unsigned long)) {
133 if (!*addr)
134 continue;
135 printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n",
136 addr, __pa(addr), *addr);
137 corruption = 1;
138 *addr = 0;
139 }
140 }
141
b43d196c 142 WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
6784f7d0
AV
143}
144
304e629b
AV
145static void check_corruption(struct work_struct *dummy);
146static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
147
148static void check_corruption(struct work_struct *dummy)
6784f7d0
AV
149{
150 check_for_bios_corruption();
304e629b 151 schedule_delayed_work(&bios_check_work,
a7bd1daf 152 round_jiffies_relative(corruption_check_period*HZ));
6784f7d0
AV
153}
154
304e629b 155static int start_periodic_check_for_corruption(void)
6784f7d0 156{
a7bd1daf 157 if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
304e629b 158 return 0;
6784f7d0
AV
159
160 printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n",
161 corruption_check_period);
162
304e629b
AV
163 /* First time we run the checks right away */
164 schedule_delayed_work(&bios_check_work, 0);
165 return 0;
6784f7d0 166}
13fe86f4 167device_initcall(start_periodic_check_for_corruption);
6784f7d0 168