Skip to content

Commit 0440feb

Browse files
tlendackybp3tk0v
authored andcommitted
x86/sev: Do RMP memory coverage check after max_pfn has been set
The RMP table is probed early in the boot process before max_pfn has been set, so the logic to check if the RMP covers all of system memory is not valid. Move the RMP memory coverage check from snp_probe_rmptable_info() into snp_rmptable_init(), which is well after max_pfn has been set. Also, fix the calculation to use PFN_UP instead of PHYS_PFN, in order to compute the required RMP size properly. Fixes: 216d106 ("x86/sev: Add SEV-SNP host initialization support") Signed-off-by: Tom Lendacky <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Link: https://lore.kernel.org/r/bec4364c7e34358cc576f01bb197a7796a109169.1718984524.git.thomas.lendacky@amd.com
1 parent 38918e0 commit 0440feb

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

arch/x86/virt/svm/sev.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static __init void snp_enable(void *arg)
120120

121121
bool snp_probe_rmptable_info(void)
122122
{
123-
u64 max_rmp_pfn, calc_rmp_sz, rmp_sz, rmp_base, rmp_end;
123+
u64 rmp_sz, rmp_base, rmp_end;
124124

125125
rdmsrl(MSR_AMD64_RMP_BASE, rmp_base);
126126
rdmsrl(MSR_AMD64_RMP_END, rmp_end);
@@ -137,28 +137,11 @@ bool snp_probe_rmptable_info(void)
137137

138138
rmp_sz = rmp_end - rmp_base + 1;
139139

140-
/*
141-
* Calculate the amount the memory that must be reserved by the BIOS to
142-
* address the whole RAM, including the bookkeeping area. The RMP itself
143-
* must also be covered.
144-
*/
145-
max_rmp_pfn = max_pfn;
146-
if (PHYS_PFN(rmp_end) > max_pfn)
147-
max_rmp_pfn = PHYS_PFN(rmp_end);
148-
149-
calc_rmp_sz = (max_rmp_pfn << 4) + RMPTABLE_CPU_BOOKKEEPING_SZ;
150-
151-
if (calc_rmp_sz > rmp_sz) {
152-
pr_err("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n",
153-
calc_rmp_sz, rmp_sz);
154-
return false;
155-
}
156-
157140
probed_rmp_base = rmp_base;
158141
probed_rmp_size = rmp_sz;
159142

160143
pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n",
161-
probed_rmp_base, probed_rmp_base + probed_rmp_size - 1);
144+
rmp_base, rmp_end);
162145

163146
return true;
164147
}
@@ -206,9 +189,8 @@ void __init snp_fixup_e820_tables(void)
206189
*/
207190
static int __init snp_rmptable_init(void)
208191
{
192+
u64 max_rmp_pfn, calc_rmp_sz, rmptable_size, rmp_end, val;
209193
void *rmptable_start;
210-
u64 rmptable_size;
211-
u64 val;
212194

213195
if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
214196
return 0;
@@ -219,10 +201,28 @@ static int __init snp_rmptable_init(void)
219201
if (!probed_rmp_size)
220202
goto nosnp;
221203

204+
rmp_end = probed_rmp_base + probed_rmp_size - 1;
205+
206+
/*
207+
* Calculate the amount the memory that must be reserved by the BIOS to
208+
* address the whole RAM, including the bookkeeping area. The RMP itself
209+
* must also be covered.
210+
*/
211+
max_rmp_pfn = max_pfn;
212+
if (PFN_UP(rmp_end) > max_pfn)
213+
max_rmp_pfn = PFN_UP(rmp_end);
214+
215+
calc_rmp_sz = (max_rmp_pfn << 4) + RMPTABLE_CPU_BOOKKEEPING_SZ;
216+
if (calc_rmp_sz > probed_rmp_size) {
217+
pr_err("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n",
218+
calc_rmp_sz, probed_rmp_size);
219+
goto nosnp;
220+
}
221+
222222
rmptable_start = memremap(probed_rmp_base, probed_rmp_size, MEMREMAP_WB);
223223
if (!rmptable_start) {
224224
pr_err("Failed to map RMP table\n");
225-
return 1;
225+
goto nosnp;
226226
}
227227

228228
/*

0 commit comments

Comments
 (0)