unaccepted_memory.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/efi.h>
  3. #include <linux/memblock.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/crash_dump.h>
  6. #include <linux/nmi.h>
  7. #include <asm/unaccepted_memory.h>
  8. /* Protects unaccepted memory bitmap and accepting_list */
  9. static DEFINE_SPINLOCK(unaccepted_memory_lock);
  10. struct accept_range {
  11. struct list_head list;
  12. unsigned long start;
  13. unsigned long end;
  14. };
  15. static LIST_HEAD(accepting_list);
  16. /*
  17. * accept_memory() -- Consult bitmap and accept the memory if needed.
  18. *
  19. * Only memory that is explicitly marked as unaccepted in the bitmap requires
  20. * an action. All the remaining memory is implicitly accepted and doesn't need
  21. * acceptance.
  22. *
  23. * No need to accept:
  24. * - anything if the system has no unaccepted table;
  25. * - memory that is below phys_base;
  26. * - memory that is above the memory that addressable by the bitmap;
  27. */
  28. void accept_memory(phys_addr_t start, unsigned long size)
  29. {
  30. struct efi_unaccepted_memory *unaccepted;
  31. unsigned long range_start, range_end;
  32. struct accept_range range, *entry;
  33. phys_addr_t end = start + size;
  34. unsigned long flags;
  35. u64 unit_size;
  36. unaccepted = efi_get_unaccepted_table();
  37. if (!unaccepted)
  38. return;
  39. unit_size = unaccepted->unit_size;
  40. /*
  41. * Only care for the part of the range that is represented
  42. * in the bitmap.
  43. */
  44. if (start < unaccepted->phys_base)
  45. start = unaccepted->phys_base;
  46. if (end < unaccepted->phys_base)
  47. return;
  48. /* Translate to offsets from the beginning of the bitmap */
  49. start -= unaccepted->phys_base;
  50. end -= unaccepted->phys_base;
  51. /*
  52. * load_unaligned_zeropad() can lead to unwanted loads across page
  53. * boundaries. The unwanted loads are typically harmless. But, they
  54. * might be made to totally unrelated or even unmapped memory.
  55. * load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now
  56. * #VE) to recover from these unwanted loads.
  57. *
  58. * But, this approach does not work for unaccepted memory. For TDX, a
  59. * load from unaccepted memory will not lead to a recoverable exception
  60. * within the guest. The guest will exit to the VMM where the only
  61. * recourse is to terminate the guest.
  62. *
  63. * There are two parts to fix this issue and comprehensively avoid
  64. * access to unaccepted memory. Together these ensure that an extra
  65. * "guard" page is accepted in addition to the memory that needs to be
  66. * used:
  67. *
  68. * 1. Implicitly extend the range_contains_unaccepted_memory(start, size)
  69. * checks up to the next unit_size if 'start+size' is aligned on a
  70. * unit_size boundary.
  71. *
  72. * 2. Implicitly extend accept_memory(start, size) to the next unit_size
  73. * if 'size+end' is aligned on a unit_size boundary. (immediately
  74. * following this comment)
  75. */
  76. if (!(end % unit_size))
  77. end += unit_size;
  78. /* Make sure not to overrun the bitmap */
  79. if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
  80. end = unaccepted->size * unit_size * BITS_PER_BYTE;
  81. range.start = start / unit_size;
  82. range.end = DIV_ROUND_UP(end, unit_size);
  83. retry:
  84. spin_lock_irqsave(&unaccepted_memory_lock, flags);
  85. /*
  86. * Check if anybody works on accepting the same range of the memory.
  87. *
  88. * The check is done with unit_size granularity. It is crucial to catch
  89. * all accept requests to the same unit_size block, even if they don't
  90. * overlap on physical address level.
  91. */
  92. list_for_each_entry(entry, &accepting_list, list) {
  93. if (entry->end <= range.start)
  94. continue;
  95. if (entry->start >= range.end)
  96. continue;
  97. /*
  98. * Somebody else accepting the range. Or at least part of it.
  99. *
  100. * Drop the lock and retry until it is complete.
  101. */
  102. spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
  103. goto retry;
  104. }
  105. /*
  106. * Register that the range is about to be accepted.
  107. * Make sure nobody else will accept it.
  108. */
  109. list_add(&range.list, &accepting_list);
  110. range_start = range.start;
  111. for_each_set_bitrange_from(range_start, range_end, unaccepted->bitmap,
  112. range.end) {
  113. unsigned long phys_start, phys_end;
  114. unsigned long len = range_end - range_start;
  115. phys_start = range_start * unit_size + unaccepted->phys_base;
  116. phys_end = range_end * unit_size + unaccepted->phys_base;
  117. /*
  118. * Keep interrupts disabled until the accept operation is
  119. * complete in order to prevent deadlocks.
  120. *
  121. * Enabling interrupts before calling arch_accept_memory()
  122. * creates an opportunity for an interrupt handler to request
  123. * acceptance for the same memory. The handler will continuously
  124. * spin with interrupts disabled, preventing other task from
  125. * making progress with the acceptance process.
  126. */
  127. spin_unlock(&unaccepted_memory_lock);
  128. arch_accept_memory(phys_start, phys_end);
  129. spin_lock(&unaccepted_memory_lock);
  130. bitmap_clear(unaccepted->bitmap, range_start, len);
  131. }
  132. list_del(&range.list);
  133. touch_softlockup_watchdog();
  134. spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
  135. }
  136. bool range_contains_unaccepted_memory(phys_addr_t start, unsigned long size)
  137. {
  138. struct efi_unaccepted_memory *unaccepted;
  139. phys_addr_t end = start + size;
  140. unsigned long flags;
  141. bool ret = false;
  142. u64 unit_size;
  143. unaccepted = efi_get_unaccepted_table();
  144. if (!unaccepted)
  145. return false;
  146. unit_size = unaccepted->unit_size;
  147. /*
  148. * Only care for the part of the range that is represented
  149. * in the bitmap.
  150. */
  151. if (start < unaccepted->phys_base)
  152. start = unaccepted->phys_base;
  153. if (end < unaccepted->phys_base)
  154. return false;
  155. /* Translate to offsets from the beginning of the bitmap */
  156. start -= unaccepted->phys_base;
  157. end -= unaccepted->phys_base;
  158. /*
  159. * Also consider the unaccepted state of the *next* page. See fix #1 in
  160. * the comment on load_unaligned_zeropad() in accept_memory().
  161. */
  162. if (!(end % unit_size))
  163. end += unit_size;
  164. /* Make sure not to overrun the bitmap */
  165. if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
  166. end = unaccepted->size * unit_size * BITS_PER_BYTE;
  167. spin_lock_irqsave(&unaccepted_memory_lock, flags);
  168. while (start < end) {
  169. if (test_bit(start / unit_size, unaccepted->bitmap)) {
  170. ret = true;
  171. break;
  172. }
  173. start += unit_size;
  174. }
  175. spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
  176. return ret;
  177. }
  178. #ifdef CONFIG_PROC_VMCORE
  179. static bool unaccepted_memory_vmcore_pfn_is_ram(struct vmcore_cb *cb,
  180. unsigned long pfn)
  181. {
  182. return !pfn_is_unaccepted_memory(pfn);
  183. }
  184. static struct vmcore_cb vmcore_cb = {
  185. .pfn_is_ram = unaccepted_memory_vmcore_pfn_is_ram,
  186. };
  187. static int __init unaccepted_memory_init_kdump(void)
  188. {
  189. register_vmcore_cb(&vmcore_cb);
  190. return 0;
  191. }
  192. core_initcall(unaccepted_memory_init_kdump);
  193. #endif /* CONFIG_PROC_VMCORE */