git: 7658d1532caa - main - vm_radix: define vm_radix_insert_lookup_lt and use in vm_page_rename

From: Ryan Libby <rlibby_at_FreeBSD.org>
Date: Thu, 06 Jun 2024 17:35:46 UTC
The branch main has been updated by rlibby:

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

commit 7658d1532caa47deafa20e4af902e0e26dd0b34c
Author:     Ryan Libby <rlibby@FreeBSD.org>
AuthorDate: 2024-06-06 17:26:50 +0000
Commit:     Ryan Libby <rlibby@FreeBSD.org>
CommitDate: 2024-06-06 17:26:50 +0000

    vm_radix: define vm_radix_insert_lookup_lt and use in vm_page_rename
    
    Use the new pctrie combined lookup/insert.  This is an easy application
    of the new facility.  There are other places where we do this for pages
    that may need more plumbing to use combined lookup/insert.
    
    Reviewed by:    kib (previous version), dougm, markj
    Sponsored by:   Dell EMC Isilon
    Differential Revision:  https://reviews.freebsd.org/D45396
---
 sys/vm/vm_page.c  |  5 +----
 sys/vm/vm_radix.h | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/sys/vm/vm_page.c b/sys/vm/vm_page.c
index 9ba31cb9e1b3..b41439045205 100644
--- a/sys/vm/vm_page.c
+++ b/sys/vm/vm_page.c
@@ -1851,9 +1851,6 @@ vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
 	VM_OBJECT_ASSERT_WLOCKED(new_object);
 
 	KASSERT(m->ref_count != 0, ("vm_page_rename: page %p has no refs", m));
-	mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
-	KASSERT(mpred == NULL || mpred->pindex != new_pindex,
-	    ("vm_page_rename: pindex already renamed"));
 
 	/*
 	 * Create a custom version of vm_page_insert() which does not depend
@@ -1862,7 +1859,7 @@ vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
 	 */
 	opidx = m->pindex;
 	m->pindex = new_pindex;
-	if (vm_radix_insert(&new_object->rtree, m)) {
+	if (vm_radix_insert_lookup_lt(&new_object->rtree, m, &mpred) != 0) {
 		m->pindex = opidx;
 		return (1);
 	}
diff --git a/sys/vm/vm_radix.h b/sys/vm/vm_radix.h
index 815c915b5fb1..dcacf1a5d3fc 100644
--- a/sys/vm/vm_radix.h
+++ b/sys/vm/vm_radix.h
@@ -69,6 +69,26 @@ vm_radix_insert(struct vm_radix *rtree, vm_page_t page)
 	return (VM_RADIX_PCTRIE_INSERT(&rtree->rt_trie, page));
 }
 
+/*
+ * Insert the page into the vm_radix tree with its pindex as the key.  Panic if
+ * the pindex already exists.  Return zero on success or a non-zero error on
+ * memory allocation failure.  Set the out parameter mpred to the previous page
+ * in the tree as if found by a previous call to vm_radix_lookup_le with the
+ * new page pindex.
+ */
+static __inline int
+vm_radix_insert_lookup_lt(struct vm_radix *rtree, vm_page_t page,
+    vm_page_t *mpred)
+{
+	int error;
+
+	error = VM_RADIX_PCTRIE_INSERT_LOOKUP_LE(&rtree->rt_trie, page, mpred);
+	if (__predict_false(error == EEXIST))
+		panic("vm_radix_insert_lookup_lt: page already present, %p",
+		    *mpred);
+	return (error);
+}
+
 /*
  * Returns the value stored at the index assuming there is an external lock.
  *