git: 1f9cc5ffc505 - main - riscv: handle kernel PTE edge-case in pmap_enter_l2()

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Thu, 06 Oct 2022 22:05:42 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=1f9cc5ffc50523498545b6f7d50406feb18903be

commit 1f9cc5ffc50523498545b6f7d50406feb18903be
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2022-10-05 16:14:36 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2022-10-06 22:04:53 +0000

    riscv: handle kernel PTE edge-case in pmap_enter_l2()
    
    Page table pages are never freed from the kernel pmap, instead they are
    zeroed when a range is unmapped. This allows future mappings to be
    constructed more quickly. Detect this scenario in pmap_enter_l2(), so we
    don't fail to create a superpage mapping when the 2MB range is actually
    available.
    
    Reviewed by:    markj
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D36885
---
 sys/riscv/riscv/pmap.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/sys/riscv/riscv/pmap.c b/sys/riscv/riscv/pmap.c
index de7f548b6519..a667bc73c95e 100644
--- a/sys/riscv/riscv/pmap.c
+++ b/sys/riscv/riscv/pmap.c
@@ -3146,6 +3146,24 @@ pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
 	    PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL, lockp));
 }
 
+/*
+ * Returns true if every page table entry in the specified page table is
+ * zero.
+ */
+static bool
+pmap_every_pte_zero(vm_paddr_t pa)
+{
+	pt_entry_t *pt_end, *pte;
+
+	KASSERT((pa & PAGE_MASK) == 0, ("pa is misaligned"));
+	pte = (pt_entry_t *)PHYS_TO_DMAP(pa);
+	for (pt_end = pte + Ln_ENTRIES; pte < pt_end; pte++) {
+		if (*pte != 0)
+			return (false);
+	}
+	return (true);
+}
+
 /*
  * Tries to create the specified 2MB page mapping.  Returns KERN_SUCCESS if
  * the mapping was created, and one of KERN_FAILURE, KERN_NO_SPACE, or
@@ -3190,7 +3208,8 @@ pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2, u_int flags,
 				    "pmap_enter_l2: no space for va %#lx"
 				    " in pmap %p", va, pmap);
 				return (KERN_NO_SPACE);
-			} else {
+			} else if (va < VM_MAXUSER_ADDRESS ||
+			    !pmap_every_pte_zero(L2PTE_TO_PHYS(oldl2))) {
 				l2pg->ref_count--;
 				CTR2(KTR_PMAP, "pmap_enter_l2:"
 				    " failed to replace existing mapping"