[PATCH] x86/mm: fix lookup_address() to handle physical memory holes in direct mapping

From: Ashish Kalra
Date: Fri Jun 28 2024 - 16:53:16 EST


From: Ashish Kalra <ashish.kalra@xxxxxxx>

lookup_address_in_pgd_attr() at pte level it is simply returning
pte_offset_kernel() and there does not seem to be a check for
returning NULL if pte_none().

Fix lookup_address_in_pgd_attr() to add check for pte_none()
after pte_offset_kernel() and return NULL if it is true.

Signed-off-by: Ashish Kalra <ashish.kalra@xxxxxxx>
---
arch/x86/mm/pat/set_memory.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 443a97e515c0..be8b5bf3bc3f 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
p4d_t *p4d;
pud_t *pud;
pmd_t *pmd;
+ pte_t *pte;

*level = PG_LEVEL_256T;
*nx = false;
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
*nx |= pmd_flags(*pmd) & _PAGE_NX;
*rw &= pmd_flags(*pmd) & _PAGE_RW;

- return pte_offset_kernel(pmd, address);
+ pte = pte_offset_kernel(pmd, address);
+ if (pte_none(*pte))
+ return NULL;
+
+ return pte;
}

/*
--
2.34.1