check.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. ret = kstrtoul(arg, 10, &val);
  28. if (ret)
  29. return ret;
  30. memory_corruption_check = val;
  31. return 0;
  32. }
  33. early_param("memory_corruption_check", set_corruption_check);
  34. static __init int set_corruption_check_period(char *arg)
  35. {
  36. ssize_t ret;
  37. unsigned long val;
  38. ret = kstrtoul(arg, 10, &val);
  39. if (ret)
  40. return ret;
  41. corruption_check_period = val;
  42. return 0;
  43. }
  44. early_param("memory_corruption_check_period", set_corruption_check_period);
  45. static __init int set_corruption_check_size(char *arg)
  46. {
  47. char *end;
  48. unsigned size;
  49. size = memparse(arg, &end);
  50. if (*end == '\0')
  51. corruption_check_size = size;
  52. return (size == corruption_check_size) ? 0 : -EINVAL;
  53. }
  54. early_param("memory_corruption_check_size", set_corruption_check_size);
  55. void __init setup_bios_corruption_check(void)
  56. {
  57. phys_addr_t start, end;
  58. u64 i;
  59. if (memory_corruption_check == -1) {
  60. memory_corruption_check =
  61. #ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
  62. 1
  63. #else
  64. 0
  65. #endif
  66. ;
  67. }
  68. if (corruption_check_size == 0)
  69. memory_corruption_check = 0;
  70. if (!memory_corruption_check)
  71. return;
  72. corruption_check_size = round_up(corruption_check_size, PAGE_SIZE);
  73. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
  74. NULL) {
  75. start = clamp_t(phys_addr_t, round_up(start, PAGE_SIZE),
  76. PAGE_SIZE, corruption_check_size);
  77. end = clamp_t(phys_addr_t, round_down(end, PAGE_SIZE),
  78. PAGE_SIZE, corruption_check_size);
  79. if (start >= end)
  80. continue;
  81. memblock_reserve(start, end - start);
  82. scan_areas[num_scan_areas].addr = start;
  83. scan_areas[num_scan_areas].size = end - start;
  84. /* Assume we've already mapped this early memory */
  85. memset(__va(start), 0, end - start);
  86. if (++num_scan_areas >= MAX_SCAN_AREAS)
  87. break;
  88. }
  89. if (num_scan_areas)
  90. printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas);
  91. }
  92. void check_for_bios_corruption(void)
  93. {
  94. int i;
  95. int corruption = 0;
  96. if (!memory_corruption_check)
  97. return;
  98. for (i = 0; i < num_scan_areas; i++) {
  99. unsigned long *addr = __va(scan_areas[i].addr);
  100. unsigned long size = scan_areas[i].size;
  101. for (; size; addr++, size -= sizeof(unsigned long)) {
  102. if (!*addr)
  103. continue;
  104. printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n",
  105. addr, __pa(addr), *addr);
  106. corruption = 1;
  107. *addr = 0;
  108. }
  109. }
  110. WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
  111. }
  112. static void check_corruption(struct work_struct *dummy);
  113. static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
  114. static void check_corruption(struct work_struct *dummy)
  115. {
  116. check_for_bios_corruption();
  117. schedule_delayed_work(&bios_check_work,
  118. round_jiffies_relative(corruption_check_period*HZ));
  119. }
  120. static int start_periodic_check_for_corruption(void)
  121. {
  122. if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
  123. return 0;
  124. printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n",
  125. corruption_check_period);
  126. /* First time we run the checks right away */
  127. schedule_delayed_work(&bios_check_work, 0);
  128. return 0;
  129. }
  130. device_initcall(start_periodic_check_for_corruption);