check.c 3.8 KB

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