init.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KMSAN initialization routines.
  4. *
  5. * Copyright (C) 2017-2021 Google LLC
  6. * Author: Alexander Potapenko <glider@google.com>
  7. *
  8. */
  9. #include "kmsan.h"
  10. #include <asm/sections.h>
  11. #include <linux/mm.h>
  12. #include <linux/memblock.h>
  13. #include "../internal.h"
  14. #define NUM_FUTURE_RANGES 128
  15. struct start_end_pair {
  16. u64 start, end;
  17. };
  18. static struct start_end_pair start_end_pairs[NUM_FUTURE_RANGES] __initdata;
  19. static int future_index __initdata;
  20. /*
  21. * Record a range of memory for which the metadata pages will be created once
  22. * the page allocator becomes available.
  23. */
  24. static void __init kmsan_record_future_shadow_range(void *start, void *end)
  25. {
  26. u64 nstart = (u64)start, nend = (u64)end, cstart, cend;
  27. bool merged = false;
  28. KMSAN_WARN_ON(future_index == NUM_FUTURE_RANGES);
  29. KMSAN_WARN_ON((nstart >= nend) ||
  30. /* Virtual address 0 is valid on s390. */
  31. (!IS_ENABLED(CONFIG_S390) && !nstart) ||
  32. !nend);
  33. nstart = ALIGN_DOWN(nstart, PAGE_SIZE);
  34. nend = ALIGN(nend, PAGE_SIZE);
  35. /*
  36. * Scan the existing ranges to see if any of them overlaps with
  37. * [start, end). In that case, merge the two ranges instead of
  38. * creating a new one.
  39. * The number of ranges is less than 20, so there is no need to organize
  40. * them into a more intelligent data structure.
  41. */
  42. for (int i = 0; i < future_index; i++) {
  43. cstart = start_end_pairs[i].start;
  44. cend = start_end_pairs[i].end;
  45. if ((cstart < nstart && cend < nstart) ||
  46. (cstart > nend && cend > nend))
  47. /* ranges are disjoint - do not merge */
  48. continue;
  49. start_end_pairs[i].start = min(nstart, cstart);
  50. start_end_pairs[i].end = max(nend, cend);
  51. merged = true;
  52. break;
  53. }
  54. if (merged)
  55. return;
  56. start_end_pairs[future_index].start = nstart;
  57. start_end_pairs[future_index].end = nend;
  58. future_index++;
  59. }
  60. /*
  61. * Initialize the shadow for existing mappings during kernel initialization.
  62. * These include kernel text/data sections, NODE_DATA and future ranges
  63. * registered while creating other data (e.g. percpu).
  64. *
  65. * Allocations via memblock can be only done before slab is initialized.
  66. */
  67. void __init kmsan_init_shadow(void)
  68. {
  69. const size_t nd_size = sizeof(pg_data_t);
  70. phys_addr_t p_start, p_end;
  71. u64 loop;
  72. int nid;
  73. for_each_reserved_mem_range(loop, &p_start, &p_end)
  74. kmsan_record_future_shadow_range(phys_to_virt(p_start),
  75. phys_to_virt(p_end));
  76. /* Allocate shadow for .data */
  77. kmsan_record_future_shadow_range(_sdata, _edata);
  78. for_each_online_node(nid)
  79. kmsan_record_future_shadow_range(
  80. NODE_DATA(nid), (char *)NODE_DATA(nid) + nd_size);
  81. for (int i = 0; i < future_index; i++)
  82. kmsan_init_alloc_meta_for_range(
  83. (void *)start_end_pairs[i].start,
  84. (void *)start_end_pairs[i].end);
  85. }
  86. struct metadata_page_pair {
  87. struct page *shadow, *origin;
  88. };
  89. static struct metadata_page_pair held_back[NR_PAGE_ORDERS] __initdata;
  90. /*
  91. * Eager metadata allocation. When the memblock allocator is freeing pages to
  92. * pagealloc, we use 2/3 of them as metadata for the remaining 1/3.
  93. * We store the pointers to the returned blocks of pages in held_back[] grouped
  94. * by their order: when kmsan_memblock_free_pages() is called for the first
  95. * time with a certain order, it is reserved as a shadow block, for the second
  96. * time - as an origin block. On the third time the incoming block receives its
  97. * shadow and origin ranges from the previously saved shadow and origin blocks,
  98. * after which held_back[order] can be used again.
  99. *
  100. * At the very end there may be leftover blocks in held_back[]. They are
  101. * collected later by kmsan_memblock_discard().
  102. */
  103. bool kmsan_memblock_free_pages(struct page *page, unsigned int order)
  104. {
  105. struct page *shadow, *origin;
  106. if (!held_back[order].shadow) {
  107. held_back[order].shadow = page;
  108. return false;
  109. }
  110. if (!held_back[order].origin) {
  111. held_back[order].origin = page;
  112. return false;
  113. }
  114. shadow = held_back[order].shadow;
  115. origin = held_back[order].origin;
  116. kmsan_setup_meta(page, shadow, origin, order);
  117. held_back[order].shadow = NULL;
  118. held_back[order].origin = NULL;
  119. return true;
  120. }
  121. #define MAX_BLOCKS 8
  122. struct smallstack {
  123. struct page *items[MAX_BLOCKS];
  124. int index;
  125. int order;
  126. };
  127. static struct smallstack collect = {
  128. .index = 0,
  129. .order = MAX_PAGE_ORDER,
  130. };
  131. static void smallstack_push(struct smallstack *stack, struct page *pages)
  132. {
  133. KMSAN_WARN_ON(stack->index == MAX_BLOCKS);
  134. stack->items[stack->index] = pages;
  135. stack->index++;
  136. }
  137. #undef MAX_BLOCKS
  138. static struct page *smallstack_pop(struct smallstack *stack)
  139. {
  140. struct page *ret;
  141. KMSAN_WARN_ON(stack->index == 0);
  142. stack->index--;
  143. ret = stack->items[stack->index];
  144. stack->items[stack->index] = NULL;
  145. return ret;
  146. }
  147. static void do_collection(void)
  148. {
  149. struct page *page, *shadow, *origin;
  150. while (collect.index >= 3) {
  151. page = smallstack_pop(&collect);
  152. shadow = smallstack_pop(&collect);
  153. origin = smallstack_pop(&collect);
  154. kmsan_setup_meta(page, shadow, origin, collect.order);
  155. __free_pages_core(page, collect.order, MEMINIT_EARLY);
  156. }
  157. }
  158. static void collect_split(void)
  159. {
  160. struct smallstack tmp = {
  161. .order = collect.order - 1,
  162. .index = 0,
  163. };
  164. struct page *page;
  165. if (!collect.order)
  166. return;
  167. while (collect.index) {
  168. page = smallstack_pop(&collect);
  169. smallstack_push(&tmp, &page[0]);
  170. smallstack_push(&tmp, &page[1 << tmp.order]);
  171. }
  172. __memcpy(&collect, &tmp, sizeof(tmp));
  173. }
  174. /*
  175. * Memblock is about to go away. Split the page blocks left over in held_back[]
  176. * and return 1/3 of that memory to the system.
  177. */
  178. static void kmsan_memblock_discard(void)
  179. {
  180. /*
  181. * For each order=N:
  182. * - push held_back[N].shadow and .origin to @collect;
  183. * - while there are >= 3 elements in @collect, do garbage collection:
  184. * - pop 3 ranges from @collect;
  185. * - use two of them as shadow and origin for the third one;
  186. * - repeat;
  187. * - split each remaining element from @collect into 2 ranges of
  188. * order=N-1,
  189. * - repeat.
  190. */
  191. collect.order = MAX_PAGE_ORDER;
  192. for (int i = MAX_PAGE_ORDER; i >= 0; i--) {
  193. if (held_back[i].shadow)
  194. smallstack_push(&collect, held_back[i].shadow);
  195. if (held_back[i].origin)
  196. smallstack_push(&collect, held_back[i].origin);
  197. held_back[i].shadow = NULL;
  198. held_back[i].origin = NULL;
  199. do_collection();
  200. collect_split();
  201. }
  202. }
  203. void __init kmsan_init_runtime(void)
  204. {
  205. /* Assuming current is init_task */
  206. kmsan_internal_task_create(current);
  207. kmsan_memblock_discard();
  208. pr_info("Starting KernelMemorySanitizer\n");
  209. pr_info("ATTENTION: KMSAN is a debugging tool! Do not use it on production machines!\n");
  210. kmsan_enabled = true;
  211. }