check.c 4.2 KB

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