xref: /freebsd/sys/vm/vm_glue.c (revision fb919e4d5a2c1baca52ac70d1064f140fffdda71)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
175929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_glue.c	8.6 (Berkeley) 1/5/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
613c4dd356SDavid Greenman  *
62c3aac50fSPeter Wemm  * $FreeBSD$
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65e9822d92SJoerg Wunsch #include "opt_rlimit.h"
66faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
67e9822d92SJoerg Wunsch 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
70fb919e4dSMark Murray #include <sys/lock.h>
71fb919e4dSMark Murray #include <sys/mutex.h>
72df8bae1dSRodney W. Grimes #include <sys/proc.h>
73df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
743aa12267SBruce Evans #include <sys/shm.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
761005a129SJohn Baldwin #include <sys/sx.h>
77ceb0cf87SJohn Dyson #include <sys/sysctl.h>
78df8bae1dSRodney W. Grimes 
7926f9a767SRodney W. Grimes #include <sys/kernel.h>
800384fff8SJason Evans #include <sys/ktr.h>
81a2a1c95cSPeter Wemm #include <sys/unistd.h>
8226f9a767SRodney W. Grimes 
83b1037dcdSBruce Evans #include <machine/limits.h>
84b1037dcdSBruce Evans 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
87efeaf95aSDavid Greenman #include <vm/pmap.h>
88efeaf95aSDavid Greenman #include <vm/vm_map.h>
89df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9026f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
91df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
92efeaf95aSDavid Greenman #include <vm/vm_extern.h>
93efeaf95aSDavid Greenman 
94efeaf95aSDavid Greenman #include <sys/user.h>
95df8bae1dSRodney W. Grimes 
962b14f991SJulian Elischer /*
972b14f991SJulian Elischer  * System initialization
982b14f991SJulian Elischer  *
992b14f991SJulian Elischer  * Note: proc0 from proc.h
1002b14f991SJulian Elischer  */
1012b14f991SJulian Elischer 
1024590fd3aSDavid Greenman static void vm_init_limits __P((void *));
1034590fd3aSDavid Greenman SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0)
1042b14f991SJulian Elischer 
1052b14f991SJulian Elischer /*
1062b14f991SJulian Elischer  * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
1072b14f991SJulian Elischer  *
1082b14f991SJulian Elischer  * Note: run scheduling should be divorced from the vm system.
1092b14f991SJulian Elischer  */
1104590fd3aSDavid Greenman static void scheduler __P((void *));
1112b14f991SJulian Elischer SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL)
1122b14f991SJulian Elischer 
1132b14f991SJulian Elischer 
114f708ef1bSPoul-Henning Kamp static void swapout __P((struct proc *));
115f708ef1bSPoul-Henning Kamp 
116df8bae1dSRodney W. Grimes int
117df8bae1dSRodney W. Grimes kernacc(addr, len, rw)
118df8bae1dSRodney W. Grimes 	caddr_t addr;
119df8bae1dSRodney W. Grimes 	int len, rw;
120df8bae1dSRodney W. Grimes {
121df8bae1dSRodney W. Grimes 	boolean_t rv;
122df8bae1dSRodney W. Grimes 	vm_offset_t saddr, eaddr;
12302c58685SPoul-Henning Kamp 	vm_prot_t prot;
124df8bae1dSRodney W. Grimes 
12512a69a6aSAlan Cox 	KASSERT((rw & (~VM_PROT_ALL)) == 0,
12602c58685SPoul-Henning Kamp 	    ("illegal ``rw'' argument to kernacc (%x)\n", rw));
12702c58685SPoul-Henning Kamp 	prot = rw;
1286cde7a16SDavid Greenman 	saddr = trunc_page((vm_offset_t)addr);
1296cde7a16SDavid Greenman 	eaddr = round_page((vm_offset_t)addr + len);
130bc0d3334SJohn Dyson 	vm_map_lock_read(kernel_map);
131df8bae1dSRodney W. Grimes 	rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot);
132bc0d3334SJohn Dyson 	vm_map_unlock_read(kernel_map);
133df8bae1dSRodney W. Grimes 	return (rv == TRUE);
134df8bae1dSRodney W. Grimes }
135df8bae1dSRodney W. Grimes 
136df8bae1dSRodney W. Grimes int
137df8bae1dSRodney W. Grimes useracc(addr, len, rw)
138df8bae1dSRodney W. Grimes 	caddr_t addr;
139df8bae1dSRodney W. Grimes 	int len, rw;
140df8bae1dSRodney W. Grimes {
141df8bae1dSRodney W. Grimes 	boolean_t rv;
14202c58685SPoul-Henning Kamp 	vm_prot_t prot;
143bc0d3334SJohn Dyson 	vm_map_t map;
144bc0d3334SJohn Dyson 	vm_map_entry_t save_hint;
145df8bae1dSRodney W. Grimes 
14612a69a6aSAlan Cox 	KASSERT((rw & (~VM_PROT_ALL)) == 0,
14702c58685SPoul-Henning Kamp 	    ("illegal ``rw'' argument to useracc (%x)\n", rw));
14802c58685SPoul-Henning Kamp 	prot = rw;
14926f9a767SRodney W. Grimes 	/*
150bbc0ec52SDavid Greenman 	 * XXX - check separately to disallow access to user area and user
151bbc0ec52SDavid Greenman 	 * page tables - they are in the map.
15226f9a767SRodney W. Grimes 	 *
1530d94caffSDavid Greenman 	 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max.  It was once
1540d94caffSDavid Greenman 	 * only used (as an end address) in trap.c.  Use it as an end address
1550d94caffSDavid Greenman 	 * here too.  This bogusness has spread.  I just fixed where it was
1560d94caffSDavid Greenman 	 * used as a max in vm_mmap.c.
15726f9a767SRodney W. Grimes 	 */
158bbc0ec52SDavid Greenman 	if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS
159bbc0ec52SDavid Greenman 	    || (vm_offset_t) addr + len < (vm_offset_t) addr) {
16026f9a767SRodney W. Grimes 		return (FALSE);
16126f9a767SRodney W. Grimes 	}
162bc0d3334SJohn Dyson 	map = &curproc->p_vmspace->vm_map;
163bc0d3334SJohn Dyson 	vm_map_lock_read(map);
164bc0d3334SJohn Dyson 	/*
165bc0d3334SJohn Dyson 	 * We save the map hint, and restore it.  Useracc appears to distort
166bc0d3334SJohn Dyson 	 * the map hint unnecessarily.
167bc0d3334SJohn Dyson 	 */
168bc0d3334SJohn Dyson 	save_hint = map->hint;
169bc0d3334SJohn Dyson 	rv = vm_map_check_protection(map,
1706cde7a16SDavid Greenman 	    trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), prot);
171bc0d3334SJohn Dyson 	map->hint = save_hint;
172bc0d3334SJohn Dyson 	vm_map_unlock_read(map);
173bc0d3334SJohn Dyson 
174df8bae1dSRodney W. Grimes 	return (rv == TRUE);
175df8bae1dSRodney W. Grimes }
176df8bae1dSRodney W. Grimes 
177df8bae1dSRodney W. Grimes void
178df8bae1dSRodney W. Grimes vslock(addr, len)
179df8bae1dSRodney W. Grimes 	caddr_t addr;
180df8bae1dSRodney W. Grimes 	u_int len;
181df8bae1dSRodney W. Grimes {
1826cde7a16SDavid Greenman 	vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page((vm_offset_t)addr),
1836cde7a16SDavid Greenman 	    round_page((vm_offset_t)addr + len), FALSE);
184df8bae1dSRodney W. Grimes }
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes void
1877de47255SPoul-Henning Kamp vsunlock(addr, len)
188df8bae1dSRodney W. Grimes 	caddr_t addr;
189df8bae1dSRodney W. Grimes 	u_int len;
190df8bae1dSRodney W. Grimes {
1916cde7a16SDavid Greenman 	vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page((vm_offset_t)addr),
1926cde7a16SDavid Greenman 	    round_page((vm_offset_t)addr + len), TRUE);
193df8bae1dSRodney W. Grimes }
194df8bae1dSRodney W. Grimes 
195df8bae1dSRodney W. Grimes /*
196df8bae1dSRodney W. Grimes  * Implement fork's actions on an address space.
197df8bae1dSRodney W. Grimes  * Here we arrange for the address space to be copied or referenced,
198df8bae1dSRodney W. Grimes  * allocate a user struct (pcb and kernel stack), then call the
199df8bae1dSRodney W. Grimes  * machine-dependent layer to fill those in and make the new process
200a2a1c95cSPeter Wemm  * ready to run.  The new process is set up so that it returns directly
201a2a1c95cSPeter Wemm  * to user mode to avoid stack copying and relocation problems.
202df8bae1dSRodney W. Grimes  */
203a2a1c95cSPeter Wemm void
204a2a1c95cSPeter Wemm vm_fork(p1, p2, flags)
205df8bae1dSRodney W. Grimes 	register struct proc *p1, *p2;
206a2a1c95cSPeter Wemm 	int flags;
207df8bae1dSRodney W. Grimes {
208df8bae1dSRodney W. Grimes 	register struct user *up;
209df8bae1dSRodney W. Grimes 
21091c28bfdSLuoqi Chen 	if ((flags & RFPROC) == 0) {
21191c28bfdSLuoqi Chen 		/*
21291c28bfdSLuoqi Chen 		 * Divorce the memory, if it is shared, essentially
21391c28bfdSLuoqi Chen 		 * this changes shared memory amongst threads, into
21491c28bfdSLuoqi Chen 		 * COW locally.
21591c28bfdSLuoqi Chen 		 */
21691c28bfdSLuoqi Chen 		if ((flags & RFMEM) == 0) {
21791c28bfdSLuoqi Chen 			if (p1->p_vmspace->vm_refcnt > 1) {
21891c28bfdSLuoqi Chen 				vmspace_unshare(p1);
21991c28bfdSLuoqi Chen 			}
22091c28bfdSLuoqi Chen 		}
22191c28bfdSLuoqi Chen 		cpu_fork(p1, p2, flags);
22291c28bfdSLuoqi Chen 		return;
22391c28bfdSLuoqi Chen 	}
22491c28bfdSLuoqi Chen 
2255856e12eSJohn Dyson 	if (flags & RFMEM) {
2265856e12eSJohn Dyson 		p2->p_vmspace = p1->p_vmspace;
2275856e12eSJohn Dyson 		p1->p_vmspace->vm_refcnt++;
2285856e12eSJohn Dyson 	}
2295856e12eSJohn Dyson 
23090ecac61SMatthew Dillon 	while (vm_page_count_severe()) {
23126f9a767SRodney W. Grimes 		VM_WAIT;
2320d94caffSDavid Greenman 	}
23326f9a767SRodney W. Grimes 
2345856e12eSJohn Dyson 	if ((flags & RFMEM) == 0) {
235df8bae1dSRodney W. Grimes 		p2->p_vmspace = vmspace_fork(p1->p_vmspace);
236df8bae1dSRodney W. Grimes 
237d4da2dbaSAlan Cox 		pmap_pinit2(vmspace_pmap(p2->p_vmspace));
238d4da2dbaSAlan Cox 
239df8bae1dSRodney W. Grimes 		if (p1->p_vmspace->vm_shm)
240dabee6feSPeter Wemm 			shmfork(p1, p2);
241a2a1c95cSPeter Wemm 	}
242df8bae1dSRodney W. Grimes 
243675878e7SJohn Dyson 	pmap_new_proc(p2);
24426f9a767SRodney W. Grimes 
245675878e7SJohn Dyson 	up = p2->p_addr;
246df8bae1dSRodney W. Grimes 
24739fb8e6bSJulian Elischer 	/*
24839fb8e6bSJulian Elischer 	 * p_stats currently points at fields in the user struct
24939fb8e6bSJulian Elischer 	 * but not at &u, instead at p_addr. Copy parts of
25039fb8e6bSJulian Elischer 	 * p_stats; zero the rest of p_stats (statistics).
251dc9c271aSJulian Elischer 	 *
252dc9c271aSJulian Elischer 	 * If procsig->ps_refcnt is 1 and p2->p_sigacts is NULL we dont' need
253dc9c271aSJulian Elischer 	 * to share sigacts, so we use the up->u_sigacts.
25439fb8e6bSJulian Elischer 	 */
25539fb8e6bSJulian Elischer 	p2->p_stats = &up->u_stats;
256dc9c271aSJulian Elischer 	if (p2->p_sigacts == NULL) {
257dc9c271aSJulian Elischer 		if (p2->p_procsig->ps_refcnt != 1)
258dc9c271aSJulian Elischer 			printf ("PID:%d NULL sigacts with refcnt not 1!\n",p2->p_pid);
259dc9c271aSJulian Elischer 		p2->p_sigacts = &up->u_sigacts;
260dc9c271aSJulian Elischer 		up->u_sigacts = *p1->p_sigacts;
261dc9c271aSJulian Elischer 	}
26288c5ea45SJulian Elischer 
263df8bae1dSRodney W. Grimes 	bzero(&up->u_stats.pstat_startzero,
264df8bae1dSRodney W. Grimes 	    (unsigned) ((caddr_t) &up->u_stats.pstat_endzero -
265df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startzero));
266df8bae1dSRodney W. Grimes 	bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy,
267df8bae1dSRodney W. Grimes 	    ((caddr_t) &up->u_stats.pstat_endcopy -
268df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startcopy));
269df8bae1dSRodney W. Grimes 
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 	/*
272a2a1c95cSPeter Wemm 	 * cpu_fork will copy and update the pcb, set up the kernel stack,
273a2a1c95cSPeter Wemm 	 * and make the child ready to run.
274df8bae1dSRodney W. Grimes 	 */
27591c28bfdSLuoqi Chen 	cpu_fork(p1, p2, flags);
276df8bae1dSRodney W. Grimes }
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes /*
279df8bae1dSRodney W. Grimes  * Set default limits for VM system.
280df8bae1dSRodney W. Grimes  * Called for proc 0, and then inherited by all others.
2812b14f991SJulian Elischer  *
2822b14f991SJulian Elischer  * XXX should probably act directly on proc0.
283df8bae1dSRodney W. Grimes  */
2842b14f991SJulian Elischer static void
2852b14f991SJulian Elischer vm_init_limits(udata)
2864590fd3aSDavid Greenman 	void *udata;
287df8bae1dSRodney W. Grimes {
288d841aaa7SBruce Evans 	register struct proc *p = udata;
289bbc0ec52SDavid Greenman 	int rss_limit;
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 	/*
2920d94caffSDavid Greenman 	 * Set up the initial limits on process VM. Set the maximum resident
2930d94caffSDavid Greenman 	 * set size to be half of (reasonably) available memory.  Since this
2940d94caffSDavid Greenman 	 * is a soft limit, it comes into effect only when the system is out
2950d94caffSDavid Greenman 	 * of memory - half of main memory helps to favor smaller processes,
296bbc0ec52SDavid Greenman 	 * and reduces thrashing of the object cache.
297df8bae1dSRodney W. Grimes 	 */
298df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
299df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
300df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
301df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
302dd0bd066SDavid Greenman 	/* limit the limit to no less than 2MB */
303f2daac0cSDavid Greenman 	rss_limit = max(cnt.v_free_count, 512);
304bbc0ec52SDavid Greenman 	p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
30526f9a767SRodney W. Grimes 	p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
3085074aecdSJohn Baldwin /*
3095074aecdSJohn Baldwin  * Must be called with the proc struc mutex held.
3105074aecdSJohn Baldwin  */
31126f9a767SRodney W. Grimes void
31226f9a767SRodney W. Grimes faultin(p)
31326f9a767SRodney W. Grimes 	struct proc *p;
31426f9a767SRodney W. Grimes {
31526f9a767SRodney W. Grimes 
3165074aecdSJohn Baldwin 	mtx_assert(&p->p_mtx, MA_OWNED);
3179ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
3185074aecdSJohn Baldwin 	if ((p->p_sflag & PS_INMEM) == 0) {
31926f9a767SRodney W. Grimes 
32026f9a767SRodney W. Grimes 		++p->p_lock;
3219ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
32245ece682SJohn Baldwin 		PROC_UNLOCK(p);
32326f9a767SRodney W. Grimes 
3245074aecdSJohn Baldwin 		mtx_assert(&Giant, MA_OWNED);
325675878e7SJohn Dyson 		pmap_swapin_proc(p);
32626f9a767SRodney W. Grimes 
32745ece682SJohn Baldwin 		PROC_LOCK(p);
3289ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
329915cf38bSJohn Baldwin 		if (p->p_stat == SRUN) {
33026f9a767SRodney W. Grimes 			setrunqueue(p);
3310384fff8SJason Evans 		}
33226f9a767SRodney W. Grimes 
3335074aecdSJohn Baldwin 		p->p_sflag |= PS_INMEM;
33426f9a767SRodney W. Grimes 
33526f9a767SRodney W. Grimes 		/* undo the effect of setting SLOCK above */
33626f9a767SRodney W. Grimes 		--p->p_lock;
33726f9a767SRodney W. Grimes 	}
3389ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
33926f9a767SRodney W. Grimes }
34026f9a767SRodney W. Grimes 
341df8bae1dSRodney W. Grimes /*
34226f9a767SRodney W. Grimes  * This swapin algorithm attempts to swap-in processes only if there
34326f9a767SRodney W. Grimes  * is enough space for them.  Of course, if a process waits for a long
34426f9a767SRodney W. Grimes  * time, it will be swapped in anyway.
3450384fff8SJason Evans  *
3460384fff8SJason Evans  * Giant is still held at this point, to be released in tsleep.
347df8bae1dSRodney W. Grimes  */
3482b14f991SJulian Elischer /* ARGSUSED*/
3492b14f991SJulian Elischer static void
350d841aaa7SBruce Evans scheduler(dummy)
351d841aaa7SBruce Evans 	void *dummy;
352df8bae1dSRodney W. Grimes {
353df8bae1dSRodney W. Grimes 	register struct proc *p;
354df8bae1dSRodney W. Grimes 	register int pri;
355df8bae1dSRodney W. Grimes 	struct proc *pp;
356df8bae1dSRodney W. Grimes 	int ppri;
357df8bae1dSRodney W. Grimes 
3580384fff8SJason Evans 	mtx_assert(&Giant, MA_OWNED);
3590384fff8SJason Evans 
360df8bae1dSRodney W. Grimes loop:
36190ecac61SMatthew Dillon 	if (vm_page_count_min()) {
3620d94caffSDavid Greenman 		VM_WAIT;
36390ecac61SMatthew Dillon 		goto loop;
3640d94caffSDavid Greenman 	}
36526f9a767SRodney W. Grimes 
366df8bae1dSRodney W. Grimes 	pp = NULL;
367df8bae1dSRodney W. Grimes 	ppri = INT_MIN;
3681005a129SJohn Baldwin 	sx_slock(&allproc_lock);
3695074aecdSJohn Baldwin 	LIST_FOREACH(p, &allproc, p_list) {
3709ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
371bd7e5f99SJohn Dyson 		if (p->p_stat == SRUN &&
3725074aecdSJohn Baldwin 			(p->p_sflag & (PS_INMEM | PS_SWAPPING)) == 0) {
3730d94caffSDavid Greenman 
374a669a6e9SJohn Dyson 			pri = p->p_swtime + p->p_slptime;
3755074aecdSJohn Baldwin 			if ((p->p_sflag & PS_SWAPINREQ) == 0) {
376a669a6e9SJohn Dyson 				pri -= p->p_nice * 8;
377a669a6e9SJohn Dyson 			}
37895461b45SJohn Dyson 
37926f9a767SRodney W. Grimes 			/*
38026f9a767SRodney W. Grimes 			 * if this process is higher priority and there is
3810d94caffSDavid Greenman 			 * enough space, then select this process instead of
3820d94caffSDavid Greenman 			 * the previous selection.
38326f9a767SRodney W. Grimes 			 */
3840d94caffSDavid Greenman 			if (pri > ppri) {
385df8bae1dSRodney W. Grimes 				pp = p;
386df8bae1dSRodney W. Grimes 				ppri = pri;
387df8bae1dSRodney W. Grimes 			}
388df8bae1dSRodney W. Grimes 		}
3899ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
390df8bae1dSRodney W. Grimes 	}
3911005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
39226f9a767SRodney W. Grimes 
393df8bae1dSRodney W. Grimes 	/*
394a669a6e9SJohn Dyson 	 * Nothing to do, back to sleep.
395df8bae1dSRodney W. Grimes 	 */
396df8bae1dSRodney W. Grimes 	if ((p = pp) == NULL) {
39724a1cce3SDavid Greenman 		tsleep(&proc0, PVM, "sched", 0);
398df8bae1dSRodney W. Grimes 		goto loop;
399df8bae1dSRodney W. Grimes 	}
4009ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
4015074aecdSJohn Baldwin 	p->p_sflag &= ~PS_SWAPINREQ;
4029ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
403a669a6e9SJohn Dyson 
404df8bae1dSRodney W. Grimes 	/*
40526f9a767SRodney W. Grimes 	 * We would like to bring someone in. (only if there is space).
406df8bae1dSRodney W. Grimes 	 */
40745ece682SJohn Baldwin 	PROC_LOCK(p);
40826f9a767SRodney W. Grimes 	faultin(p);
40945ece682SJohn Baldwin 	PROC_UNLOCK(p);
4109ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
411df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
4129ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
413df8bae1dSRodney W. Grimes 	goto loop;
414df8bae1dSRodney W. Grimes }
415df8bae1dSRodney W. Grimes 
4165afce282SDavid Greenman #ifndef NO_SWAPPING
4175afce282SDavid Greenman 
418ceb0cf87SJohn Dyson /*
419ceb0cf87SJohn Dyson  * Swap_idle_threshold1 is the guaranteed swapped in time for a process
420ceb0cf87SJohn Dyson  */
421303b270bSEivind Eklund static int swap_idle_threshold1 = 2;
422ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1,
423ceb0cf87SJohn Dyson 	CTLFLAG_RW, &swap_idle_threshold1, 0, "");
424ceb0cf87SJohn Dyson 
425ceb0cf87SJohn Dyson /*
426ceb0cf87SJohn Dyson  * Swap_idle_threshold2 is the time that a process can be idle before
427ceb0cf87SJohn Dyson  * it will be swapped out, if idle swapping is enabled.
428ceb0cf87SJohn Dyson  */
429303b270bSEivind Eklund static int swap_idle_threshold2 = 10;
430ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2,
431ceb0cf87SJohn Dyson 	CTLFLAG_RW, &swap_idle_threshold2, 0, "");
432ceb0cf87SJohn Dyson 
433df8bae1dSRodney W. Grimes /*
434df8bae1dSRodney W. Grimes  * Swapout is driven by the pageout daemon.  Very simple, we find eligible
435df8bae1dSRodney W. Grimes  * procs and unwire their u-areas.  We try to always "swap" at least one
436df8bae1dSRodney W. Grimes  * process in case we need the room for a swapin.
437df8bae1dSRodney W. Grimes  * If any procs have been sleeping/stopped for at least maxslp seconds,
438df8bae1dSRodney W. Grimes  * they are swapped.  Else, we swap the longest-sleeping or stopped process,
439df8bae1dSRodney W. Grimes  * if any, otherwise the longest-resident process.
440df8bae1dSRodney W. Grimes  */
441df8bae1dSRodney W. Grimes void
4423a2dc656SJohn Dyson swapout_procs(action)
4433a2dc656SJohn Dyson int action;
444df8bae1dSRodney W. Grimes {
445df8bae1dSRodney W. Grimes 	register struct proc *p;
446df8bae1dSRodney W. Grimes 	struct proc *outp, *outp2;
447df8bae1dSRodney W. Grimes 	int outpri, outpri2;
448df8bae1dSRodney W. Grimes 	int didswap = 0;
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes 	outp = outp2 = NULL;
45126f9a767SRodney W. Grimes 	outpri = outpri2 = INT_MIN;
4521005a129SJohn Baldwin 	sx_slock(&allproc_lock);
4530d94caffSDavid Greenman retry:
4545074aecdSJohn Baldwin 	LIST_FOREACH(p, &allproc, p_list) {
455b18bfc3dSJohn Dyson 		struct vmspace *vm;
456b18bfc3dSJohn Dyson 
4575074aecdSJohn Baldwin 		PROC_LOCK(p);
45869b40456SJohn Baldwin 		if (p->p_lock != 0 ||
45969b40456SJohn Baldwin 		    (p->p_flag & (P_TRACED|P_SYSTEM|P_WEXIT)) != 0) {
4605074aecdSJohn Baldwin 			PROC_UNLOCK(p);
4615074aecdSJohn Baldwin 			continue;
4625074aecdSJohn Baldwin 		}
463b18bfc3dSJohn Dyson 		vm = p->p_vmspace;
4645074aecdSJohn Baldwin 		PROC_UNLOCK(p);
4659ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
46669b40456SJohn Baldwin 		if ((p->p_sflag & (PS_INMEM|PS_SWAPPING)) != PS_INMEM) {
4679ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
46869b40456SJohn Baldwin 			continue;
46969b40456SJohn Baldwin 		}
47069b40456SJohn Baldwin 
471df8bae1dSRodney W. Grimes 		switch (p->p_stat) {
4720d94caffSDavid Greenman 		default:
4739ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
474df8bae1dSRodney W. Grimes 			continue;
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes 		case SSLEEP:
477df8bae1dSRodney W. Grimes 		case SSTOP:
47826f9a767SRodney W. Grimes 			/*
479bfbfac11SDavid Greenman 			 * do not swapout a realtime process
480bfbfac11SDavid Greenman 			 */
481d5a08a60SJake Burkholder 			if (PRI_IS_REALTIME(p->p_pri.pri_class)) {
4829ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
483bfbfac11SDavid Greenman 				continue;
484c8a6b001SJohn Baldwin 			}
485bfbfac11SDavid Greenman 
486bfbfac11SDavid Greenman 			/*
487ceb0cf87SJohn Dyson 			 * Do not swapout a process waiting on a critical
488ceb0cf87SJohn Dyson 			 * event of some kind.  Also guarantee swap_idle_threshold1
489ceb0cf87SJohn Dyson 			 * time in memory.
4900d94caffSDavid Greenman 			 */
491d5a08a60SJake Burkholder 			if (((p->p_pri.pri_level) < PSOCK) ||
492c8a6b001SJohn Baldwin 				(p->p_slptime < swap_idle_threshold1)) {
4939ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
494ceb0cf87SJohn Dyson 				continue;
495c8a6b001SJohn Baldwin 			}
496ceb0cf87SJohn Dyson 
497ceb0cf87SJohn Dyson 			/*
498ceb0cf87SJohn Dyson 			 * If the system is under memory stress, or if we are swapping
499ceb0cf87SJohn Dyson 			 * idle processes >= swap_idle_threshold2, then swap the process
500ceb0cf87SJohn Dyson 			 * out.
501ceb0cf87SJohn Dyson 			 */
502ceb0cf87SJohn Dyson 			if (((action & VM_SWAP_NORMAL) == 0) &&
503ceb0cf87SJohn Dyson 				(((action & VM_SWAP_IDLE) == 0) ||
5045074aecdSJohn Baldwin 				  (p->p_slptime < swap_idle_threshold2))) {
5059ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
5060d94caffSDavid Greenman 				continue;
5075074aecdSJohn Baldwin 			}
5089ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
5090d94caffSDavid Greenman 
510b18bfc3dSJohn Dyson 			++vm->vm_refcnt;
511d3a34985SJohn Dyson 			/*
512d3a34985SJohn Dyson 			 * do not swapout a process that is waiting for VM
513d3a34985SJohn Dyson 			 * data structures there is a possible deadlock.
514d3a34985SJohn Dyson 			 */
515996c772fSJohn Dyson 			if (lockmgr(&vm->vm_map.lock,
516996c772fSJohn Dyson 					LK_EXCLUSIVE | LK_NOWAIT,
517996c772fSJohn Dyson 					(void *)0, curproc)) {
518b18bfc3dSJohn Dyson 				vmspace_free(vm);
519d3a34985SJohn Dyson 				continue;
520d3a34985SJohn Dyson 			}
521b18bfc3dSJohn Dyson 			vm_map_unlock(&vm->vm_map);
52211b224dcSDavid Greenman 			/*
5230d94caffSDavid Greenman 			 * If the process has been asleep for awhile and had
5240d94caffSDavid Greenman 			 * most of its pages taken away already, swap it out.
52511b224dcSDavid Greenman 			 */
5269ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
527ceb0cf87SJohn Dyson 			if ((action & VM_SWAP_NORMAL) ||
528ceb0cf87SJohn Dyson 				((action & VM_SWAP_IDLE) &&
529ceb0cf87SJohn Dyson 				 (p->p_slptime > swap_idle_threshold2))) {
5309ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
531df8bae1dSRodney W. Grimes 				swapout(p);
532b18bfc3dSJohn Dyson 				vmspace_free(vm);
533df8bae1dSRodney W. Grimes 				didswap++;
5340d94caffSDavid Greenman 				goto retry;
5355074aecdSJohn Baldwin 			} else
5369ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
53726f9a767SRodney W. Grimes 		}
538ceb0cf87SJohn Dyson 	}
5391005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
54026f9a767SRodney W. Grimes 	/*
54126f9a767SRodney W. Grimes 	 * If we swapped something out, and another process needed memory,
54226f9a767SRodney W. Grimes 	 * then wakeup the sched process.
54326f9a767SRodney W. Grimes 	 */
5440d94caffSDavid Greenman 	if (didswap)
54524a1cce3SDavid Greenman 		wakeup(&proc0);
546df8bae1dSRodney W. Grimes }
547df8bae1dSRodney W. Grimes 
548f708ef1bSPoul-Henning Kamp static void
549df8bae1dSRodney W. Grimes swapout(p)
550df8bae1dSRodney W. Grimes 	register struct proc *p;
551df8bae1dSRodney W. Grimes {
552df8bae1dSRodney W. Grimes 
553d3a34985SJohn Dyson #if defined(SWAP_DEBUG)
554d3a34985SJohn Dyson 	printf("swapping out %d\n", p->p_pid);
555d3a34985SJohn Dyson #endif
55626f9a767SRodney W. Grimes 	++p->p_stats->p_ru.ru_nswap;
557df8bae1dSRodney W. Grimes 	/*
55826f9a767SRodney W. Grimes 	 * remember the process resident count
559df8bae1dSRodney W. Grimes 	 */
560b1028ad1SLuoqi Chen 	p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes 	(void) splhigh();
5639ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
5645074aecdSJohn Baldwin 	p->p_sflag &= ~PS_INMEM;
5655074aecdSJohn Baldwin 	p->p_sflag |= PS_SWAPPING;
566df8bae1dSRodney W. Grimes 	if (p->p_stat == SRUN)
5678928d440SPeter Wemm 		remrunqueue(p);
5689ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
569df8bae1dSRodney W. Grimes 	(void) spl0();
57026f9a767SRodney W. Grimes 
571675878e7SJohn Dyson 	pmap_swapout_proc(p);
5726d40c3d3SDavid Greenman 
5739ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
5745074aecdSJohn Baldwin 	p->p_sflag &= ~PS_SWAPPING;
575df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
5769ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
577df8bae1dSRodney W. Grimes }
5785afce282SDavid Greenman #endif /* !NO_SWAPPING */
579