check.c 3.8 KB

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