check.c 4.2 KB

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