Index: sys/uvm/uvm_map.c
===================================================================
RCS file: /cvsroot/src/sys/uvm/uvm_map.c,v
retrieving revision 1.394
diff -p -u -r1.394 uvm_map.c
--- sys/uvm/uvm_map.c	10 Apr 2022 09:50:46 -0000	1.394
+++ sys/uvm/uvm_map.c	24 Apr 2022 11:14:58 -0000
@@ -1798,7 +1798,7 @@ uvm_map_findspace(struct vm_map *map, va
 {
 	struct vm_map_entry *entry;
 	struct vm_map_entry *child, *prev, *tmp;
-	vaddr_t orig_hint __diagused;
+//	vaddr_t orig_hint __diagused;
 	const int topdown = map->flags & VM_MAP_TOPDOWN;
 	UVMHIST_FUNC(__func__);
 	UVMHIST_CALLARGS(maphist, "(map=%#jx, hint=%#jx, len=%ju, flags=%#jx...",
@@ -1818,7 +1818,7 @@ uvm_map_findspace(struct vm_map *map, va
 	 * we fail the first time.
 	 */
 
-	orig_hint = hint;
+//	orig_hint = hint;
 	if (hint < vm_map_min(map)) {	/* check ranges ... */
 		if (flags & UVM_FLAG_FIXED) {
 			UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0);
@@ -1940,8 +1940,11 @@ nextgap:
 	KDASSERT((flags & UVM_FLAG_FIXED) == 0);
 	/* If there is not enough space in the whole tree, we fail */
 	tmp = ROOT_ENTRY(map);
-	if (tmp == NULL || tmp->maxgap < length)
+	if (tmp == NULL || tmp->maxgap < length) {
+		UVMHIST_LOG(maphist, "<- larger than max gap",
+		    0,0,0,0);
 		goto notfound;
+	}
 
 	prev = NULL; /* previous candidate */
 
@@ -2008,14 +2011,14 @@ nextgap:
 			goto listsearch;
 	}
 	if (prev == NULL)
-		goto notfound;
+		goto listsearch;
 
 	if (topdown) {
-		KASSERT(orig_hint >= prev->next->start - length ||
-		    prev->next->start - length > prev->next->start);
+//		KASSERT(orig_hint >= prev->next->start - length ||
+//		    prev->next->start - length > prev->next->start);
 		hint = prev->next->start - length;
 	} else {
-		KASSERT(orig_hint <= prev->end);
+//		KASSERT(orig_hint <= prev->end);
 		hint = prev->end;
 	}
 	switch (uvm_map_space_avail(&hint, length, uoffset, align,
@@ -2052,11 +2055,11 @@ nextgap:
 	}
 
 	if (topdown) {
-		KASSERT(orig_hint >= tmp->next->start - length ||
-		    tmp->next->start - length > tmp->next->start);
+//		KASSERT(orig_hint >= tmp->next->start - length ||
+//		    tmp->next->start - length > tmp->next->start);
 		hint = tmp->next->start - length;
 	} else {
-		KASSERT(orig_hint <= tmp->end);
+//		KASSERT(orig_hint <= tmp->end);
 		hint = tmp->end;
 	}
 	switch (uvm_map_space_avail(&hint, length, uoffset, align,
@@ -2080,11 +2083,14 @@ nextgap:
 	 *	 entry->next->start = VA of end of current gap
 	 */
 
+	entry = map->first_free;
+	if (entry == &map->header)
+		entry = topdown ? map->header.prev : map->header.next;
+
 	for (;;) {
 		/* Update hint for current gap. */
 		hint = topdown ? entry->next->start - length : entry->end;
 
-		/* See if it fits. */
 		switch (uvm_map_space_avail(&hint, length, uoffset, align,
 		    flags, topdown, entry)) {
 		case 1:
@@ -2098,7 +2104,7 @@ nextgap:
 			if (entry == &map->header) {
 				UVMHIST_LOG(maphist, "<- failed (off start)",
 				    0,0,0,0);
-				goto notfound;
+				return (NULL);
 			}
 			entry = entry->prev;
 		} else {
@@ -2106,31 +2112,31 @@ nextgap:
 			if (entry == &map->header) {
 				UVMHIST_LOG(maphist, "<- failed (off end)",
 				    0,0,0,0);
-				goto notfound;
+				return (NULL);
 			}
 		}
 	}
+	/* not reached */
+
+ notfound:
+	UVMHIST_LOG(maphist, "<- failed (notfound)", 0,0,0,0);
+
+	return (NULL);
 
  found:
 	SAVE_HINT(map, map->hint, entry);
 	*result = hint;
 	UVMHIST_LOG(maphist,"<- got it!  (result=%#jx)", hint, 0,0,0);
-	KASSERTMSG( topdown || hint >= orig_hint, "hint: %#jx, orig_hint: %#jx",
-	    (uintmax_t)hint, (uintmax_t)orig_hint);
-	KASSERTMSG(!topdown || hint <= orig_hint, "hint: %#jx, orig_hint: %#jx",
-	    (uintmax_t)hint, (uintmax_t)orig_hint);
+//	KASSERTMSG( topdown || hint >= orig_hint, "hint: %#jx, orig_hint: %#jx",
+//	    (uintmax_t)hint, (uintmax_t)orig_hint);
+//	KASSERTMSG(!topdown || hint <= orig_hint, "hint: %#jx, orig_hint: %#jx",
+//	    (uintmax_t)hint, (uintmax_t)orig_hint);
 	KASSERT(entry->end <= hint);
 	KASSERT(hint + length <= entry->next->start);
 	return (entry);
 
  wraparound:
 	UVMHIST_LOG(maphist, "<- failed (wrap around)", 0,0,0,0);
-
-	return (NULL);
-
- notfound:
-	UVMHIST_LOG(maphist, "<- failed (notfound)", 0,0,0,0);
-
 	return (NULL);
 }
 
