1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/memblock.h>
5 #include <linux/spinlock.h>
6 #include <linux/crash_dump.h>
7 #include <asm/unaccepted_memory.h>
9 /* Protects unaccepted memory bitmap and accepting_list */
10 static DEFINE_SPINLOCK(unaccepted_memory_lock);
13 struct list_head list;
18 static LIST_HEAD(accepting_list);
21 * accept_memory() -- Consult bitmap and accept the memory if needed.
23 * Only memory that is explicitly marked as unaccepted in the bitmap requires
24 * an action. All the remaining memory is implicitly accepted and doesn't need
28 * - anything if the system has no unaccepted table;
29 * - memory that is below phys_base;
30 * - memory that is above the memory that addressable by the bitmap;
32 void accept_memory(phys_addr_t start, phys_addr_t end)
34 struct efi_unaccepted_memory *unaccepted;
35 unsigned long range_start, range_end;
36 struct accept_range range, *entry;
40 unaccepted = efi_get_unaccepted_table();
44 unit_size = unaccepted->unit_size;
47 * Only care for the part of the range that is represented
50 if (start < unaccepted->phys_base)
51 start = unaccepted->phys_base;
52 if (end < unaccepted->phys_base)
55 /* Translate to offsets from the beginning of the bitmap */
56 start -= unaccepted->phys_base;
57 end -= unaccepted->phys_base;
60 * load_unaligned_zeropad() can lead to unwanted loads across page
61 * boundaries. The unwanted loads are typically harmless. But, they
62 * might be made to totally unrelated or even unmapped memory.
63 * load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now
64 * #VE) to recover from these unwanted loads.
66 * But, this approach does not work for unaccepted memory. For TDX, a
67 * load from unaccepted memory will not lead to a recoverable exception
68 * within the guest. The guest will exit to the VMM where the only
69 * recourse is to terminate the guest.
71 * There are two parts to fix this issue and comprehensively avoid
72 * access to unaccepted memory. Together these ensure that an extra
73 * "guard" page is accepted in addition to the memory that needs to be
76 * 1. Implicitly extend the range_contains_unaccepted_memory(start, end)
77 * checks up to end+unit_size if 'end' is aligned on a unit_size
80 * 2. Implicitly extend accept_memory(start, end) to end+unit_size if
81 * 'end' is aligned on a unit_size boundary. (immediately following
84 if (!(end % unit_size))
87 /* Make sure not to overrun the bitmap */
88 if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
89 end = unaccepted->size * unit_size * BITS_PER_BYTE;
91 range.start = start / unit_size;
92 range.end = DIV_ROUND_UP(end, unit_size);
94 spin_lock_irqsave(&unaccepted_memory_lock, flags);
97 * Check if anybody works on accepting the same range of the memory.
99 * The check is done with unit_size granularity. It is crucial to catch
100 * all accept requests to the same unit_size block, even if they don't
101 * overlap on physical address level.
103 list_for_each_entry(entry, &accepting_list, list) {
104 if (entry->end < range.start)
106 if (entry->start >= range.end)
110 * Somebody else accepting the range. Or at least part of it.
112 * Drop the lock and retry until it is complete.
114 spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
119 * Register that the range is about to be accepted.
120 * Make sure nobody else will accept it.
122 list_add(&range.list, &accepting_list);
124 range_start = range.start;
125 for_each_set_bitrange_from(range_start, range_end, unaccepted->bitmap,
127 unsigned long phys_start, phys_end;
128 unsigned long len = range_end - range_start;
130 phys_start = range_start * unit_size + unaccepted->phys_base;
131 phys_end = range_end * unit_size + unaccepted->phys_base;
134 * Keep interrupts disabled until the accept operation is
135 * complete in order to prevent deadlocks.
137 * Enabling interrupts before calling arch_accept_memory()
138 * creates an opportunity for an interrupt handler to request
139 * acceptance for the same memory. The handler will continuously
140 * spin with interrupts disabled, preventing other task from
141 * making progress with the acceptance process.
143 spin_unlock(&unaccepted_memory_lock);
145 arch_accept_memory(phys_start, phys_end);
147 spin_lock(&unaccepted_memory_lock);
148 bitmap_clear(unaccepted->bitmap, range_start, len);
151 list_del(&range.list);
152 spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
155 bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end)
157 struct efi_unaccepted_memory *unaccepted;
162 unaccepted = efi_get_unaccepted_table();
166 unit_size = unaccepted->unit_size;
169 * Only care for the part of the range that is represented
172 if (start < unaccepted->phys_base)
173 start = unaccepted->phys_base;
174 if (end < unaccepted->phys_base)
177 /* Translate to offsets from the beginning of the bitmap */
178 start -= unaccepted->phys_base;
179 end -= unaccepted->phys_base;
182 * Also consider the unaccepted state of the *next* page. See fix #1 in
183 * the comment on load_unaligned_zeropad() in accept_memory().
185 if (!(end % unit_size))
188 /* Make sure not to overrun the bitmap */
189 if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
190 end = unaccepted->size * unit_size * BITS_PER_BYTE;
192 spin_lock_irqsave(&unaccepted_memory_lock, flags);
193 while (start < end) {
194 if (test_bit(start / unit_size, unaccepted->bitmap)) {
201 spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
206 #ifdef CONFIG_PROC_VMCORE
207 static bool unaccepted_memory_vmcore_pfn_is_ram(struct vmcore_cb *cb,
210 return !pfn_is_unaccepted_memory(pfn);
213 static struct vmcore_cb vmcore_cb = {
214 .pfn_is_ram = unaccepted_memory_vmcore_pfn_is_ram,
217 static int __init unaccepted_memory_init_kdump(void)
219 register_vmcore_cb(&vmcore_cb);
222 core_initcall(unaccepted_memory_init_kdump);
223 #endif /* CONFIG_PROC_VMCORE */