check.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <linux/module.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, MAX_NUMNODES, &start, &end, NULL) {
  73. start = clamp_t(phys_addr_t, round_up(start, PAGE_SIZE),
  74. PAGE_SIZE, corruption_check_size);
  75. end = clamp_t(phys_addr_t, round_down(end, PAGE_SIZE),
  76. PAGE_SIZE, corruption_check_size);
  77. if (start >= end)
  78. continue;
  79. memblock_reserve(start, end - start);
  80. scan_areas[num_scan_areas].addr = start;
  81. scan_areas[num_scan_areas].size = end - start;
  82. /* Assume we've already mapped this early memory */
  83. memset(__va(start), 0, end - start);
  84. if (++num_scan_areas >= MAX_SCAN_AREAS)
  85. break;
  86. }
  87. if (num_scan_areas)
  88. printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas);
  89. }
  90. void check_for_bios_corruption(void)
  91. {
  92. int i;
  93. int corruption = 0;
  94. if (!memory_corruption_check)
  95. return;
  96. for (i = 0; i < num_scan_areas; i++) {
  97. unsigned long *addr = __va(scan_areas[i].addr);
  98. unsigned long size = scan_areas[i].size;
  99. for (; size; addr++, size -= sizeof(unsigned long)) {
  100. if (!*addr)
  101. continue;
  102. printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n",
  103. addr, __pa(addr), *addr);
  104. corruption = 1;
  105. *addr = 0;
  106. }
  107. }
  108. WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
  109. }
  110. static void check_corruption(struct work_struct *dummy);
  111. static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
  112. static void check_corruption(struct work_struct *dummy)
  113. {
  114. check_for_bios_corruption();
  115. schedule_delayed_work(&bios_check_work,
  116. round_jiffies_relative(corruption_check_period*HZ));
  117. }
  118. static int start_periodic_check_for_corruption(void)
  119. {
  120. if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
  121. return 0;
  122. printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n",
  123. corruption_check_period);
  124. /* First time we run the checks right away */
  125. schedule_delayed_work(&bios_check_work, 0);
  126. return 0;
  127. }
  128. module_init(start_periodic_check_for_corruption);