In preparation for support of a segmented RMP table, map only the RMP
table entries. The RMP bookkeeping area is only ever accessed when
first enabling SNP and does not need to remain mapped. To accomplish
this, split the initialization of the RMP bookkeeping area and the
initialization of the RMP entry area. The RMP bookkeeping area will be
mapped only while it is being initialized.
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
---
arch/x86/virt/svm/sev.c | 36 +++++++++++++++++++++++++++++++-----
1 file changed, 31 insertions(+), 5 deletions(-)
diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c
index 31d1510ae119..81e21d833cf0 100644
--- a/arch/x86/virt/svm/sev.c
+++ b/arch/x86/virt/svm/sev.c
@@ -168,6 +168,23 @@ void __init snp_fixup_e820_tables(void)
__snp_fixup_e820_tables(probed_rmp_base + probed_rmp_size);
}
+static bool __init init_rmptable_bookkeeping(void)
+{
+ void *bk;
+
+ bk = memremap(probed_rmp_base, RMPTABLE_CPU_BOOKKEEPING_SZ, MEMREMAP_WB);
+ if (!bk) {
+ pr_err("Failed to map RMP bookkeeping area\n");
+ return false;
+ }
+
+ memset(bk, 0, RMPTABLE_CPU_BOOKKEEPING_SZ);
+
+ memunmap(bk);
+
+ return true;
+}
+
/*
* Do the necessary preparations which are verified by the firmware as
* described in the SNP_INIT_EX firmware command description in the SNP
@@ -205,12 +222,17 @@ static int __init snp_rmptable_init(void)
goto nosnp;
}
- rmptable_start = memremap(probed_rmp_base, probed_rmp_size, MEMREMAP_WB);
+ /* Map only the RMP entries */
+ rmptable_start = memremap(probed_rmp_base + RMPTABLE_CPU_BOOKKEEPING_SZ,
+ probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ,
+ MEMREMAP_WB);
if (!rmptable_start) {
pr_err("Failed to map RMP table\n");
goto nosnp;
}
+ rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ;
+
/*
* Check if SEV-SNP is already enabled, this can happen in case of
* kexec boot.
@@ -219,7 +241,14 @@ static int __init snp_rmptable_init(void)
if (val & MSR_AMD64_SYSCFG_SNP_EN)
goto skip_enable;
- memset(rmptable_start, 0, probed_rmp_size);
+ /* Zero out the RMP bookkeeping area */
+ if (!init_rmptable_bookkeeping()) {
+ memunmap(rmptable_start);
+ goto nosnp;
+ }
+
+ /* Zero out the RMP entries */
+ memset(rmptable_start, 0, rmptable_size);
/* Flush the caches to ensure that data is written before SNP is enabled. */
wbinvd_on_all_cpus();
@@ -230,9 +259,6 @@ static int __init snp_rmptable_init(void)
on_each_cpu(snp_enable, NULL, 1);
skip_enable:
- rmptable_start += RMPTABLE_CPU_BOOKKEEPING_SZ;
- rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ;
-
rmptable = (struct rmpentry_raw *)rmptable_start;
rmptable_max_pfn = rmptable_size / sizeof(struct rmpentry_raw) - 1;
--
2.43.2
> /* > * Do the necessary preparations which are verified by the firmware as > * described in the SNP_INIT_EX firmware command description in the SNP > @@ -205,12 +222,17 @@ static int __init snp_rmptable_init(void) > goto nosnp; > } > > - rmptable_start = memremap(probed_rmp_base, probed_rmp_size, MEMREMAP_WB); > + /* Map only the RMP entries */ > + rmptable_start = memremap(probed_rmp_base + RMPTABLE_CPU_BOOKKEEPING_SZ, > + probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ, > + MEMREMAP_WB); > if (!rmptable_start) { > pr_err("Failed to map RMP table\n"); > goto nosnp; > } > > + rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ; > + Nit: Move this assignment above 'rmptable_start = memremap(...)', so that rmptable_size can be used there. - Neeraj
On 10/17/24 23:38, Neeraj Upadhyay wrote: > > > >> /* >> * Do the necessary preparations which are verified by the firmware as >> * described in the SNP_INIT_EX firmware command description in the SNP >> @@ -205,12 +222,17 @@ static int __init snp_rmptable_init(void) >> goto nosnp; >> } >> >> - rmptable_start = memremap(probed_rmp_base, probed_rmp_size, MEMREMAP_WB); >> + /* Map only the RMP entries */ >> + rmptable_start = memremap(probed_rmp_base + RMPTABLE_CPU_BOOKKEEPING_SZ, >> + probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ, >> + MEMREMAP_WB); >> if (!rmptable_start) { >> pr_err("Failed to map RMP table\n"); >> goto nosnp; >> } >> >> + rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ; >> + > > Nit: Move this assignment above 'rmptable_start = memremap(...)', so that > rmptable_size can be used there. I like the symmetry of the base and size adjustment in the memremap(). To me it looks very obvious as to what is occurring. Thanks, Tom > > > - Neeraj >
On 9/30/2024 8:52 PM, Tom Lendacky wrote: > In preparation for support of a segmented RMP table, map only the RMP s/support of/supporting/ > table entries. The RMP bookkeeping area is only ever accessed when > first enabling SNP and does not need to remain mapped. To accomplish > this, split the initialization of the RMP bookkeeping area and the > initialization of the RMP entry area. The RMP bookkeeping area will be > mapped only while it is being initialized. > > Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com> Reviewed-by: Nikunj A Dadhania <nikunj@amd.com> > --- > arch/x86/virt/svm/sev.c | 36 +++++++++++++++++++++++++++++++----- > 1 file changed, 31 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c > index 31d1510ae119..81e21d833cf0 100644 > --- a/arch/x86/virt/svm/sev.c > +++ b/arch/x86/virt/svm/sev.c > @@ -168,6 +168,23 @@ void __init snp_fixup_e820_tables(void) > __snp_fixup_e820_tables(probed_rmp_base + probed_rmp_size); > } > > +static bool __init init_rmptable_bookkeeping(void) > +{ > + void *bk; > + > + bk = memremap(probed_rmp_base, RMPTABLE_CPU_BOOKKEEPING_SZ, MEMREMAP_WB); > + if (!bk) { > + pr_err("Failed to map RMP bookkeeping area\n"); > + return false; > + } > + > + memset(bk, 0, RMPTABLE_CPU_BOOKKEEPING_SZ); > + > + memunmap(bk); > + > + return true; > +} > + > /* > * Do the necessary preparations which are verified by the firmware as > * described in the SNP_INIT_EX firmware command description in the SNP > @@ -205,12 +222,17 @@ static int __init snp_rmptable_init(void) > goto nosnp; > } > > - rmptable_start = memremap(probed_rmp_base, probed_rmp_size, MEMREMAP_WB); > + /* Map only the RMP entries */ > + rmptable_start = memremap(probed_rmp_base + RMPTABLE_CPU_BOOKKEEPING_SZ, > + probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ, > + MEMREMAP_WB); > if (!rmptable_start) { > pr_err("Failed to map RMP table\n"); > goto nosnp; > } > > + rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ; > + > /* > * Check if SEV-SNP is already enabled, this can happen in case of > * kexec boot. > @@ -219,7 +241,14 @@ static int __init snp_rmptable_init(void) > if (val & MSR_AMD64_SYSCFG_SNP_EN) > goto skip_enable; > > - memset(rmptable_start, 0, probed_rmp_size); > + /* Zero out the RMP bookkeeping area */ > + if (!init_rmptable_bookkeeping()) { > + memunmap(rmptable_start); > + goto nosnp; > + } > + > + /* Zero out the RMP entries */ > + memset(rmptable_start, 0, rmptable_size); > > /* Flush the caches to ensure that data is written before SNP is enabled. */ > wbinvd_on_all_cpus(); > @@ -230,9 +259,6 @@ static int __init snp_rmptable_init(void) > on_each_cpu(snp_enable, NULL, 1); > > skip_enable: > - rmptable_start += RMPTABLE_CPU_BOOKKEEPING_SZ; > - rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ; > - > rmptable = (struct rmpentry_raw *)rmptable_start; > rmptable_max_pfn = rmptable_size / sizeof(struct rmpentry_raw) - 1; >
© 2016 - 2024 Red Hat, Inc.