Re: [PATCH Part2 RFC v4 05/40] x86/sev: Add RMP entry lookup helpers
From: Sean Christopherson
Date: Thu Jul 15 2021 - 14:37:38 EST
On Wed, Jul 07, 2021, Brijesh Singh wrote:
> The snp_lookup_page_in_rmptable() can be used by the host to read the RMP
> entry for a given page. The RMP entry format is documented in AMD PPR, see
> https://bugzilla.kernel.org/attachment.cgi?id=296015.
Ewwwwww, the RMP format isn't architectural!?
Architecturally the format of RMP entries are not specified in APM. In order
to assist software, the following table specifies select portions of the RMP
entry format for this specific product.
I know we generally don't want to add infrastructure without good reason, but on
the other hand exposing a microarchitectural data structure to the kernel at large
is going to be a disaster if the format does change on a future processor.
Looking at the future patches, dump_rmpentry() is the only power user, e.g.
everything else mostly looks at "assigned" and "level" (and one ratelimited warn
on "validated" in snp_make_page_shared(), but I suspect that particular check
can and should be dropped).
So, what about hiding "struct rmpentry" and possibly renaming it to something
scary/microarchitectural, e.g. something like
/*
* Returns 1 if the RMP entry is assigned, 0 if it exists but is not assigned,
* and -errno if there is no corresponding RMP entry.
*/
int snp_lookup_rmpentry(struct page *page, int *level)
{
unsigned long phys = page_to_pfn(page) << PAGE_SHIFT;
struct rmpentry *entry, *large_entry;
unsigned long vaddr;
if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP))
return -ENXIO;
vaddr = rmptable_start + rmptable_page_offset(phys);
if (unlikely(vaddr > rmptable_end))
return -EXNIO;
entry = (struct rmpentry *)vaddr;
/* Read a large RMP entry to get the correct page level used in RMP entry. */
vaddr = rmptable_start + rmptable_page_offset(phys & PMD_MASK);
large_entry = (struct rmpentry *)vaddr;
*level = RMP_TO_X86_PG_LEVEL(rmpentry_pagesize(large_entry));
return !!entry->assigned;
}
And then move dump_rmpentry() (or add a helper) in sev.c so that "struct rmpentry"
can be declared in sev.c.
> Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
> ---
> arch/x86/include/asm/sev.h | 4 +--
> arch/x86/kernel/sev.c | 26 +++++++++++++++++++
> include/linux/sev.h | 51 ++++++++++++++++++++++++++++++++++++++
> 3 files changed, 78 insertions(+), 3 deletions(-)
> create mode 100644 include/linux/sev.h
>
> diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
> index 6c23e694a109..9e7e7e737f55 100644
> --- a/arch/x86/include/asm/sev.h
> +++ b/arch/x86/include/asm/sev.h
> @@ -9,6 +9,7 @@
> #define __ASM_ENCRYPTED_STATE_H
>
> #include <linux/types.h>
> +#include <linux/sev.h>
Why move things to linux/sev.h? AFAICT, even at the end of the series, the only
users of anything in this file all reside somewhere in arch/x86.
> #include <asm/insn.h>
> #include <asm/sev-common.h>
> #include <asm/bootparam.h>
> @@ -75,9 +76,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
> /* Software defined (when rFlags.CF = 1) */
> #define PVALIDATE_FAIL_NOUPDATE 255
>
> -/* RMP page size */
> -#define RMP_PG_SIZE_4K 0
> -
> #define RMPADJUST_VMSA_PAGE_BIT BIT(16)
>
> #ifdef CONFIG_AMD_MEM_ENCRYPT
> diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
> index f9d813d498fa..1aed3d53f59f 100644
> --- a/arch/x86/kernel/sev.c
> +++ b/arch/x86/kernel/sev.c
> @@ -49,6 +49,8 @@
> #define DR7_RESET_VALUE 0x400
>
> #define RMPTABLE_ENTRIES_OFFSET 0x4000
> +#define RMPENTRY_SHIFT 8
> +#define rmptable_page_offset(x) (RMPTABLE_ENTRIES_OFFSET + (((unsigned long)x) >> RMPENTRY_SHIFT))
>
> /* For early boot hypervisor communication in SEV-ES enabled guests */
> static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
> @@ -2319,3 +2321,27 @@ static int __init snp_rmptable_init(void)
> * passthough state, and it is available after subsys_initcall().
> */
> fs_initcall(snp_rmptable_init);
> +
> +struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level)
Maybe just snp_get_rmpentry? Or snp_lookup_rmpentry? I'm guessing the name was
chosen to align with e.g. lookup_address_in_mm, but IMO the lookup_address helpers
are oddly named.
> +{
> + unsigned long phys = page_to_pfn(page) << PAGE_SHIFT;
> + struct rmpentry *entry, *large_entry;
> + unsigned long vaddr;
> +
> + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP))
> + return NULL;
> +
> + vaddr = rmptable_start + rmptable_page_offset(phys);
> + if (unlikely(vaddr > rmptable_end))
> + return NULL;
> +
> + entry = (struct rmpentry *)vaddr;
> +
> + /* Read a large RMP entry to get the correct page level used in RMP entry. */
> + vaddr = rmptable_start + rmptable_page_offset(phys & PMD_MASK);
> + large_entry = (struct rmpentry *)vaddr;
> + *level = RMP_TO_X86_PG_LEVEL(rmpentry_pagesize(large_entry));
> +
> + return entry;
> +}