"SIGBUG"?
+
+ if (unlikely(!cpu_feature_enabled(X86_FEATURE_SEV_SNP)))
+ return RMP_FAULT_KILL;
Shouldn't this be a WARN_ON_ONCE()? How can we get RMP faults without
SEV-SNP?
+ /* Get the native page level */
+ pte = lookup_address_in_mm(current->mm, address, &level);
+ if (unlikely(!pte))
+ return RMP_FAULT_KILL;
What would this mean? There was an RMP fault on a non-present page?
How could that happen? What if there was a race between an unmapping
event and the RMP fault delivery?
+ pfn = pte_pfn(*pte);
+ if (level > PG_LEVEL_4K) {
+ mask = pages_per_hpage(level) - pages_per_hpage(level - 1);
+ pfn |= (address >> PAGE_SHIFT) & mask;
+ }
This looks inherently racy. What happens if there are two parallel RMP
faults on the same 2M page. One of them splits the page tables, the
other gets a fault for an already-split page table.
> Is that handled here somehow?
+ /* Get the page level from the RMP entry. */
+ e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level);
+ if (!e)
+ return RMP_FAULT_KILL;
The snp_lookup_page_in_rmptable() failure cases looks WARN-worthly.
Either you're doing a lookup for something not *IN* the RMP table, or
you don't support SEV-SNP, in which case you shouldn't be in this code
in the first place.
+ /*
+ * Check if the RMP violation is due to the guest private page access.
+ * We can not resolve this RMP fault, ask to kill the guest.
+ */
+ if (rmpentry_assigned(e))
+ return RMP_FAULT_KILL;
No "We's", please. Speak in imperative voice.
+ /*
+ * The backing page level is higher than the RMP page level, request
+ * to split the page.
+ */
+ if (level > rmp_level)
+ return RMP_FAULT_PAGE_SPLIT;
This can theoretically trigger on a hugetlbfs page. Right?
I thought I asked about this before... more below...
+ return RMP_FAULT_RETRY;(struct pt_regs *regs,
+}
+
/*
* Handle faults in the user portion of the address space. Nothing in here
* should check X86_PF_USER without a specific justification: for almost
@@ -1298,6 +1350,7 @@ void do_user_addr_fault(struct pt_regs *regs,
struct task_struct *tsk;
struct mm_struct *mm;
vm_fault_t fault;
+ int ret;
unsigned int flags = FAULT_FLAG_DEFAULT;
tsk = current;
@@ -1378,6 +1431,22 @@ void
if (error_code & X86_PF_INSTR)
flags |= FAULT_FLAG_INSTRUCTION;
+ /*
+ * If its an RMP violation, try resolving it.
+ */
+ if (error_code & X86_PF_RMP) {
+ ret = handle_user_rmp_page_fault(error_code, address);
+ if (ret == RMP_FAULT_PAGE_SPLIT) {
+ flags |= FAULT_FLAG_PAGE_SPLIT;
+ } else if (ret == RMP_FAULT_KILL) {
+ fault |= VM_FAULT_SIGBUS;
+ do_sigbus(regs, error_code, address, fault);
+ return;
+ } else {
+ return;
+ }
+ }
Why not just have handle_user_rmp_page_fault() return a VM_FAULT_* code
directly?
I also suspect you can just set VM_FAULT_SIGBUS and let the do_sigbus()
call later on in the function do its work.
+static int handle_split_page_fault(struct vm_fault *vmf)
+{
+ if (!IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT))
+ return VM_FAULT_SIGBUS;
+
+ __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
+ return 0;
+}
What will this do when you hand it a hugetlbfs page?