check.c 4.2 KB

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