git: 4539de92bce9 - stable/13 - riscv: Handle invalid L2 entries in pmap_extract()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 13 Oct 2022 00:51:20 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=4539de92bce9afef8a041a0c6b6a1a078a49fe71

commit 4539de92bce9afef8a041a0c6b6a1a078a49fe71
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-09-29 17:07:26 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-10-13 00:44:03 +0000

    riscv: Handle invalid L2 entries in pmap_extract()
    
    While here, eliminate a single-use local variable.
    
    PR:             266103
    Reviewed by:    mhorne
    
    (cherry picked from commit ec21f85ab5f03a803884cc7bafa88621c613f4ca)
---
 sys/riscv/riscv/pmap.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/sys/riscv/riscv/pmap.c b/sys/riscv/riscv/pmap.c
index 412c2484f1e8..e925beb91c14 100644
--- a/sys/riscv/riscv/pmap.c
+++ b/sys/riscv/riscv/pmap.c
@@ -903,27 +903,25 @@ vm_paddr_t
 pmap_extract(pmap_t pmap, vm_offset_t va)
 {
 	pd_entry_t *l2p, l2;
-	pt_entry_t *l3p, l3;
+	pt_entry_t *l3p;
 	vm_paddr_t pa;
 
 	pa = 0;
-	PMAP_LOCK(pmap);
+
 	/*
-	 * Start with the l2 tabel. We are unable to allocate
-	 * pages in the l1 table.
+	 * Start with an L2 lookup, L1 superpages are currently not implemented.
 	 */
+	PMAP_LOCK(pmap);
 	l2p = pmap_l2(pmap, va);
-	if (l2p != NULL) {
-		l2 = pmap_load(l2p);
-		if ((l2 & PTE_RX) == 0) {
+	if (l2p != NULL && ((l2 = pmap_load(l2p)) & PTE_V) != 0) {
+		if ((l2 & PTE_RWX) == 0) {
 			l3p = pmap_l2_to_l3(l2p, va);
 			if (l3p != NULL) {
-				l3 = pmap_load(l3p);
-				pa = PTE_TO_PHYS(l3);
+				pa = PTE_TO_PHYS(pmap_load(l3p));
 				pa |= (va & L3_OFFSET);
 			}
 		} else {
-			/* L2 is superpages */
+			/* L2 is a superpage mapping. */
 			pa = L2PTE_TO_PHYS(l2);
 			pa |= (va & L2_OFFSET);
 		}