check.c 4.1 KB

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