diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index 804de18abcc2b47e7cf4e880a9b23ee159091061..0be9f9c59aa6b72d1cf55a0819be146d78f56e61 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -148,8 +148,8 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr,
 	/*
 	 * Don't allow anybody to remap normal RAM that we're using..
 	 */
-	for (pfn = phys_addr >> PAGE_SHIFT;
-				(pfn << PAGE_SHIFT) < last_addr; pfn++) {
+	for (pfn = phys_addr >> PAGE_SHIFT; pfn < max_pfn_mapped &&
+		(pfn << PAGE_SHIFT) < last_addr; pfn++) {
 
 		int is_ram = page_is_ram(pfn);