1 // SPDX-License-Identifier: GPL-2.0
3 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 #include <linux/init.h>
6 #include <linux/sched.h>
7 #include <linux/kthread.h>
8 #include <linux/workqueue.h>
9 #include <linux/memblock.h>
11 #include <asm/proto.h>
12 #include <asm/setup.h>
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
20 #define MAX_SCAN_AREAS 8
22 static int __read_mostly memory_corruption_check
= -1;
24 static unsigned __read_mostly corruption_check_size
= 64*1024;
25 static unsigned __read_mostly corruption_check_period
= 60; /* seconds */
27 static struct scan_area
{
30 } scan_areas
[MAX_SCAN_AREAS
];
31 static int num_scan_areas
;
33 static __init
int set_corruption_check(char *arg
)
39 pr_err("memory_corruption_check config string not provided\n");
43 ret
= kstrtoul(arg
, 10, &val
);
47 memory_corruption_check
= val
;
51 early_param("memory_corruption_check", set_corruption_check
);
53 static __init
int set_corruption_check_period(char *arg
)
59 pr_err("memory_corruption_check_period config string not provided\n");
63 ret
= kstrtoul(arg
, 10, &val
);
67 corruption_check_period
= val
;
70 early_param("memory_corruption_check_period", set_corruption_check_period
);
72 static __init
int set_corruption_check_size(char *arg
)
78 pr_err("memory_corruption_check_size config string not provided\n");
82 size
= memparse(arg
, &end
);
85 corruption_check_size
= size
;
87 return (size
== corruption_check_size
) ? 0 : -EINVAL
;
89 early_param("memory_corruption_check_size", set_corruption_check_size
);
92 void __init
setup_bios_corruption_check(void)
94 phys_addr_t start
, end
;
97 if (memory_corruption_check
== -1) {
98 memory_corruption_check
=
99 #ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
107 if (corruption_check_size
== 0)
108 memory_corruption_check
= 0;
110 if (!memory_corruption_check
)
113 corruption_check_size
= round_up(corruption_check_size
, PAGE_SIZE
);
115 for_each_free_mem_range(i
, NUMA_NO_NODE
, MEMBLOCK_NONE
, &start
, &end
,
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
);
124 memblock_reserve(start
, end
- start
);
125 scan_areas
[num_scan_areas
].addr
= start
;
126 scan_areas
[num_scan_areas
].size
= end
- start
;
128 /* Assume we've already mapped this early memory */
129 memset(__va(start
), 0, end
- start
);
131 if (++num_scan_areas
>= MAX_SCAN_AREAS
)
136 pr_info("Scanning %d areas for low memory corruption\n", num_scan_areas
);
140 static void check_for_bios_corruption(void)
145 if (!memory_corruption_check
)
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
;
152 for (; size
; addr
++, size
-= sizeof(unsigned long)) {
155 pr_err("Corrupted low memory at %p (%lx phys) = %08lx\n", addr
, __pa(addr
), *addr
);
161 WARN_ONCE(corruption
, KERN_ERR
"Memory corruption detected in low memory\n");
164 static void check_corruption(struct work_struct
*dummy
);
165 static DECLARE_DELAYED_WORK(bios_check_work
, check_corruption
);
167 static void check_corruption(struct work_struct
*dummy
)
169 check_for_bios_corruption();
170 schedule_delayed_work(&bios_check_work
,
171 round_jiffies_relative(corruption_check_period
*HZ
));
174 static int start_periodic_check_for_corruption(void)
176 if (!num_scan_areas
|| !memory_corruption_check
|| corruption_check_period
== 0)
179 pr_info("Scanning for low memory corruption every %d seconds\n", corruption_check_period
);
181 /* First time we run the checks right away */
182 schedule_delayed_work(&bios_check_work
, 0);
186 device_initcall(start_periodic_check_for_corruption
);