Index: sys/vm/vm_page.c =================================================================== --- sys/vm/vm_page.c +++ sys/vm/vm_page.c @@ -1324,22 +1324,17 @@ vm_page_t vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex) { - vm_page_t mold, mpred; + vm_page_t mold; VM_OBJECT_ASSERT_WLOCKED(object); + KASSERT(mnew->object == NULL, + ("vm_page_replace: page already in object")); /* * This function mostly follows vm_page_insert() and * vm_page_remove() without the radix, object count and vnode * dance. Double check such functions for more comments. */ - mpred = vm_radix_lookup(&object->rtree, pindex); - KASSERT(mpred != NULL, - ("vm_page_replace: replacing page not present with pindex")); - mpred = TAILQ_PREV(mpred, respgs, listq); - if (mpred != NULL) - KASSERT(mpred->pindex < pindex, - ("vm_page_insert_after: mpred doesn't precede pindex")); mnew->object = object; mnew->pindex = pindex; @@ -1347,17 +1342,17 @@ KASSERT(mold->queue == PQ_NONE, ("vm_page_replace: mold is on a paging queue")); - /* Detach the old page from the resident tailq. */ + /* Keep the resident page list in sorted order. */ + TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq); TAILQ_REMOVE(&object->memq, mold, listq); mold->object = NULL; vm_page_xunbusy(mold); - /* Insert the new page in the resident tailq. */ - if (mpred != NULL) - TAILQ_INSERT_AFTER(&object->memq, mpred, mnew, listq); - else - TAILQ_INSERT_HEAD(&object->memq, mnew, listq); + /* + * The object's resident_page_count does not change because we have + * swapped one page for another, but OBJ_MIGHTBEDIRTY. + */ if (pmap_page_is_write_mapped(mnew)) vm_object_set_writeable_dirty(object); return (mold);