Module Name:    src
Committed By:   martin
Date:           Sat Apr  1 16:03:48 UTC 2023

Modified Files:
        src/sys/uvm [netbsd-9]: uvm_map.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1623):

        sys/uvm/uvm_map.c: revision 1.396

uvm(9): Fix mmap optimization for topdown case.

PR kern/51393


To generate a diff of this commit:
cvs rdiff -u -r1.362.2.3 -r1.362.2.4 src/sys/uvm/uvm_map.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/uvm/uvm_map.c
diff -u src/sys/uvm/uvm_map.c:1.362.2.3 src/sys/uvm/uvm_map.c:1.362.2.4
--- src/sys/uvm/uvm_map.c:1.362.2.3	Sat Apr  1 16:00:28 2023
+++ src/sys/uvm/uvm_map.c	Sat Apr  1 16:03:48 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: uvm_map.c,v 1.362.2.3 2023/04/01 16:00:28 martin Exp $	*/
+/*	$NetBSD: uvm_map.c,v 1.362.2.4 2023/04/01 16:03:48 martin Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.362.2.3 2023/04/01 16:00:28 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.362.2.4 2023/04/01 16:03:48 martin Exp $");
 
 #include "opt_ddb.h"
 #include "opt_pax.h"
@@ -1940,7 +1940,36 @@ uvm_map_findspace(struct vm_map *map, va
 	 * it, there would be four cases).
 	 */
 
-	if ((flags & UVM_FLAG_FIXED) == 0 && hint == vm_map_min(map)) {
+	if ((flags & UVM_FLAG_FIXED) == 0 &&
+	    hint == (topdown ? vm_map_max(map) : vm_map_min(map))) {
+		/*
+		 * The uvm_map_findspace algorithm is monotonic -- for
+		 * topdown VM it starts with a high hint and returns a
+		 * lower free address; for !topdown VM it starts with a
+		 * low hint and returns a higher free address.  As an
+		 * optimization, start with the first (highest for
+		 * topdown, lowest for !topdown) free address.
+		 *
+		 * XXX This `optimization' probably doesn't actually do
+		 * much in practice unless userland explicitly passes
+		 * the VM map's minimum or maximum address, which
+		 * varies from machine to machine (VM_MAX/MIN_ADDRESS,
+		 * e.g. 0x7fbfdfeff000 on amd64 but 0xfffffffff000 on
+		 * aarch64) and may vary according to other factors
+		 * like sysctl vm.user_va0_disable.  In particular, if
+		 * the user specifies 0 as a hint to mmap, then mmap
+		 * will choose a default address which is usually _not_
+		 * VM_MAX/MIN_ADDRESS but something else instead like
+		 * VM_MAX_ADDRESS - stack size - guard page overhead,
+		 * in which case this branch is never hit.
+		 *
+		 * In fact, this branch appears to have been broken for
+		 * two decades between when topdown was introduced in
+		 * ~2003 and when it was adapted to handle the topdown
+		 * case without violating the monotonicity assertion in
+		 * 2022.  Maybe Someone^TM should either ditch the
+		 * optimization or find a better way to do it.
+		 */
 		entry = map->first_free;
 	} else {
 		if (uvm_map_lookup_entry(map, hint, &entry)) {

Reply via email to