diff --git a/kalloc.c b/kalloc.c
index 8c9ff93d3a7befe1f14836678c08140c8fd9c6f8..43e80e80a273aa2217eaf7c84cb06cee6bb9bacf 100644
--- a/kalloc.c
+++ b/kalloc.c
@@ -24,10 +24,9 @@ kinit(void)
   extern char end[];
 
   initlock(&kmem.lock, "kmem");
-  char *p1 = (char*)PGROUNDUP((uint)end);
-  char *p2 = PGROUNDDOWN(PHYSTOP);
-  for( ; p1 < p2; p1 += 4096)
-    kfree(p1);
+  char *p = (char*)PGROUNDUP((uint)end);
+  for( ; p + PGSIZE - 1 < (char*) PHYSTOP; p += PGSIZE)
+    kfree(p);
 }
 
 // Free the page of physical memory pointed at by v,