Subject: New MAP_WIRED flag to mmap(2)
To: None <tech-kern@netbsd.org>
From: Jason Thorpe <thorpej@wasabisystems.com>
List: tech-kern
Date: 10/06/2003 17:14:35
--Apple-Mail-2--868353927
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

Folks...

The following patch as a MAP_WIRED flag to mmap(2).  The new flag 
causes the mapping to be wired in memory as if by mlock(2).  This can 
be very useful for some applications that use mmap() to perform 
zero-copy I/O.

As you can see, the patch is quite simple; the infrastructure for this 
is already in place for mlockall()'s "all future mappings" case.

         -- Jason R. Thorpe <thorpej@wasabisystems.com>

--Apple-Mail-2--868353927
Content-Disposition: attachment;
	filename=map-wired.patch
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="map-wired.patch"

Index: lib/libc/sys/mmap.2
===================================================================
RCS file: /cvsroot/src/lib/libc/sys/mmap.2,v
retrieving revision 1.34
diff -c -r1.34 mmap.2
*** lib/libc/sys/mmap.2	7 Aug 2003 16:44:02 -0000	1.34
--- lib/libc/sys/mmap.2	6 Oct 2003 22:05:21 -0000
***************
*** 166,171 ****
--- 166,174 ----
  is
  .Fa NULL ,
  this flag is ignored and the system will select a mapping address.
+ .It Dv MAP_WIRED
+ Lock the mapped region into memory as with
+ .Xr mlock 2 .
  .It Dv MAP_PRIVATE
  Modifications made by this process are private, however modifications made by
  other processes using
Index: sys/sys/mman.h
===================================================================
RCS file: /cvsroot/src/sys/sys/mman.h,v
retrieving revision 1.33
diff -c -r1.33 mman.h
*** sys/sys/mman.h	7 Aug 2003 16:34:08 -0000	1.33
--- sys/sys/mman.h	6 Oct 2003 22:05:22 -0000
***************
*** 88,93 ****
--- 88,94 ----
  #define	MAP_INHERIT	 0x0080	/* region is retained after exec */
  #define	MAP_HASSEMAPHORE 0x0200	/* region may contain semaphores */
  #define	MAP_TRYFIXED     0x0400 /* attempt hint address, even within break */
+ #define	MAP_WIRED	 0x0800	/* mlock() mapping when it is established */
  
  /*
   * Mapping type
Index: sys/uvm/uvm_mmap.c
===================================================================
RCS file: /cvsroot/src/sys/uvm/uvm_mmap.c,v
retrieving revision 1.77
diff -c -r1.77 uvm_mmap.c
*** sys/uvm/uvm_mmap.c	24 Aug 2003 18:12:25 -0000	1.77
--- sys/uvm/uvm_mmap.c	6 Oct 2003 22:05:23 -0000
***************
*** 330,335 ****
--- 330,345 ----
  	if ((ssize_t) size < 0)
  		return (EINVAL);			/* don't allow wrap */
  
+ #ifndef pmap_wired_count
+ 	/*
+ 	 * if we're going to wire the mapping, restrict it to superuser.
+ 	 */
+ 	
+ 	if ((flags & MAP_WIRED) != 0 &&
+ 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
+ 		return (error);
+ #endif
+ 
  	/*
  	 * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
  	 */
***************
*** 1173,1178 ****
--- 1183,1190 ----
  	/*
  	 * POSIX 1003.1b -- if our address space was configured
  	 * to lock all future mappings, wire the one we just made.
+ 	 *
+ 	 * Also handle the MAP_WIRED flag here.
  	 */
  
  	if (prot == VM_PROT_NONE) {
***************
*** 1184,1190 ****
  		return (0);
  	}
  	vm_map_lock(map);
! 	if (map->flags & VM_MAP_WIREFUTURE) {
  		if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax
  #ifdef pmap_wired_count
  		    || (locklimit != 0 && (size +
--- 1196,1202 ----
  		return (0);
  	}
  	vm_map_lock(map);
! 	if ((flags & MAP_WIRED) != 0 || (map->flags & VM_MAP_WIREFUTURE) != 0) {
  		if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax
  #ifdef pmap_wired_count
  		    || (locklimit != 0 && (size +

--Apple-Mail-2--868353927--