git: c3d743a67a7c - main - vm_object: use reclaim callback to free pages

From: Doug Moore <dougm_at_FreeBSD.org>
Date: Thu, 19 Sep 2024 20:24:19 UTC
The branch main has been updated by dougm:

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

commit c3d743a67a7c238645efe2979c40e423ae32f2ed
Author:     Doug Moore <dougm@FreeBSD.org>
AuthorDate: 2024-09-19 20:23:19 +0000
Commit:     Doug Moore <dougm@FreeBSD.org>
CommitDate: 2024-09-19 20:23:19 +0000

    vm_object: use reclaim callback to free pages
    
    Instead of iterating over object->memq to free pages, use a callback
    in VM_RADIX_PCTRIE_RECLAIM_CALLBACK to do it.
    
    Reviewed by:    rlibby
    Differential Revision:  https://reviews.freebsd.org/D45588
---
 sys/vm/vm_object.c | 62 ++++++++++++++++++++++++++++--------------------------
 sys/vm/vm_radix.h  | 10 +++++----
 2 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/sys/vm/vm_object.c b/sys/vm/vm_object.c
index 7b3950b6f3bc..86f9271341df 100644
--- a/sys/vm/vm_object.c
+++ b/sys/vm/vm_object.c
@@ -876,6 +876,27 @@ vm_object_backing_collapse_wait(vm_object_t object)
 	return (backing_object);
 }
 
+/*
+ *	vm_object_terminate_single_page removes a pageable page from the object,
+ *	and removes it from the paging queues and frees it, if it is not wired.
+ *	It is invoked via callback from vm_object_terminate_pages.
+ */
+static void
+vm_object_terminate_single_page(vm_page_t p, void *objectv)
+{
+	vm_object_t object __diagused = objectv;
+
+	vm_page_assert_unbusied(p);
+	KASSERT(p->object == object &&
+	    (p->ref_count & VPRC_OBJREF) != 0,
+	    ("%s: page %p is inconsistent", __func__, p));
+	p->object = NULL;
+	if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) {
+		VM_CNT_INC(v_pfree);
+		vm_page_free(p);
+	}
+}
+
 /*
  *	vm_object_terminate_pages removes any remaining pageable pages
  *	from the object and resets the object to an empty state.
@@ -883,41 +904,22 @@ vm_object_backing_collapse_wait(vm_object_t object)
 static void
 vm_object_terminate_pages(vm_object_t object)
 {
-	vm_page_t p, p_next;
-
 	VM_OBJECT_ASSERT_WLOCKED(object);
 
-	/*
-	 * Free any remaining pageable pages.  This also removes them from the
-	 * paging queues.  However, don't free wired pages, just remove them
-	 * from the object.  Rather than incrementally removing each page from
-	 * the object, the page and object are reset to any empty state. 
-	 */
-	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
-		vm_page_assert_unbusied(p);
-		KASSERT(p->object == object &&
-		    (p->ref_count & VPRC_OBJREF) != 0,
-		    ("vm_object_terminate_pages: page %p is inconsistent", p));
-
-		p->object = NULL;
-		if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) {
-			VM_CNT_INC(v_pfree);
-			vm_page_free(p);
-		}
-	}
-
 	/*
 	 * If the object contained any pages, then reset it to an empty state.
-	 * None of the object's fields, including "resident_page_count", were
-	 * modified by the preceding loop.
+	 * Rather than incrementally removing each page from the object, the
+	 * page and object are reset to any empty state.
 	 */
-	if (object->resident_page_count != 0) {
-		vm_radix_reclaim_allnodes(&object->rtree);
-		TAILQ_INIT(&object->memq);
-		object->resident_page_count = 0;
-		if (object->type == OBJT_VNODE)
-			vdrop(object->handle);
-	}
+	if (object->resident_page_count == 0)
+		return;
+
+	vm_radix_reclaim_callback(&object->rtree,
+	    vm_object_terminate_single_page, object);
+	TAILQ_INIT(&object->memq);
+	object->resident_page_count = 0;
+	if (object->type == OBJT_VNODE)
+		vdrop(object->handle);
 }
 
 /*
diff --git a/sys/vm/vm_radix.h b/sys/vm/vm_radix.h
index f2e297b10641..d35c9a171d4b 100644
--- a/sys/vm/vm_radix.h
+++ b/sys/vm/vm_radix.h
@@ -187,14 +187,16 @@ vm_radix_remove(struct vm_radix *rtree, vm_pindex_t index)
 {
 	return (VM_RADIX_PCTRIE_REMOVE_LOOKUP(&rtree->rt_trie, index));
 }
-
+ 
 /*
- * Reclaim all the interior nodes from the radix tree.
+ * Reclaim all the interior nodes of the trie, and invoke the callback
+ * on all the pages, in order.
  */
 static __inline void
-vm_radix_reclaim_allnodes(struct vm_radix *rtree)
+vm_radix_reclaim_callback(struct vm_radix *rtree,
+    void (*page_cb)(vm_page_t, void *), void *arg)
 {
-	VM_RADIX_PCTRIE_RECLAIM(&rtree->rt_trie);
+	VM_RADIX_PCTRIE_RECLAIM_CALLBACK(&rtree->rt_trie, page_cb, arg);
 }
 
 /*