xref: /freebsd/sys/vm/vm_glue.c (revision ce8660e395812c6a91616a64388605aada81f5c7)
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.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
63874651b1SDavid E. O'Brien #include <sys/cdefs.h>
64874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
65874651b1SDavid E. O'Brien 
66faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
6715a7ad60SPeter Wemm #include "opt_kstack_pages.h"
6815a7ad60SPeter Wemm #include "opt_kstack_max_pages.h"
69e9822d92SJoerg Wunsch 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
72104a9b7eSAlexander Kabaev #include <sys/limits.h>
73fb919e4dSMark Murray #include <sys/lock.h>
74fb919e4dSMark Murray #include <sys/mutex.h>
75df8bae1dSRodney W. Grimes #include <sys/proc.h>
76df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
773aa12267SBruce Evans #include <sys/shm.h>
78efeaf95aSDavid Greenman #include <sys/vmmeter.h>
791005a129SJohn Baldwin #include <sys/sx.h>
80ceb0cf87SJohn Dyson #include <sys/sysctl.h>
81df8bae1dSRodney W. Grimes 
8226f9a767SRodney W. Grimes #include <sys/kernel.h>
830384fff8SJason Evans #include <sys/ktr.h>
84a2a1c95cSPeter Wemm #include <sys/unistd.h>
8526f9a767SRodney W. Grimes 
86df8bae1dSRodney W. Grimes #include <vm/vm.h>
87efeaf95aSDavid Greenman #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9126f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
92a136efe9SPeter Wemm #include <vm/vm_object.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
94efeaf95aSDavid Greenman #include <vm/vm_extern.h>
95a136efe9SPeter Wemm #include <vm/vm_pager.h>
9692da00bbSMatthew Dillon #include <vm/swap_pager.h>
97efeaf95aSDavid Greenman 
98efeaf95aSDavid Greenman #include <sys/user.h>
99df8bae1dSRodney W. Grimes 
100ea754954SJohn Baldwin extern int maxslp;
101ea754954SJohn Baldwin 
1022b14f991SJulian Elischer /*
1032b14f991SJulian Elischer  * System initialization
1042b14f991SJulian Elischer  *
1052b14f991SJulian Elischer  * Note: proc0 from proc.h
1062b14f991SJulian Elischer  */
10711caded3SAlfred Perlstein static void vm_init_limits(void *);
1084590fd3aSDavid Greenman SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0)
1092b14f991SJulian Elischer 
1102b14f991SJulian Elischer /*
1112b14f991SJulian Elischer  * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
1122b14f991SJulian Elischer  *
1132b14f991SJulian Elischer  * Note: run scheduling should be divorced from the vm system.
1142b14f991SJulian Elischer  */
11511caded3SAlfred Perlstein static void scheduler(void *);
1169a44a82bSBruce Evans SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_ANY, scheduler, NULL)
1172b14f991SJulian Elischer 
118e50f5c2eSBruce Evans #ifndef NO_SWAPPING
11911caded3SAlfred Perlstein static void swapout(struct proc *);
120a136efe9SPeter Wemm static void vm_proc_swapin(struct proc *p);
121a136efe9SPeter Wemm static void vm_proc_swapout(struct proc *p);
122e50f5c2eSBruce Evans #endif
123f708ef1bSPoul-Henning Kamp 
12443a90f3aSAlan Cox /*
12543a90f3aSAlan Cox  * MPSAFE
1262d5c7e45SMatthew Dillon  *
1272d5c7e45SMatthew Dillon  * WARNING!  This code calls vm_map_check_protection() which only checks
1282d5c7e45SMatthew Dillon  * the associated vm_map_entry range.  It does not determine whether the
1292d5c7e45SMatthew Dillon  * contents of the memory is actually readable or writable.  In most cases
1302d5c7e45SMatthew Dillon  * just checking the vm_map_entry is sufficient within the kernel's address
1312d5c7e45SMatthew Dillon  * space.
13243a90f3aSAlan Cox  */
133df8bae1dSRodney W. Grimes int
134df8bae1dSRodney W. Grimes kernacc(addr, len, rw)
135c3dfdfd1SAlfred Perlstein 	void *addr;
136df8bae1dSRodney W. Grimes 	int len, rw;
137df8bae1dSRodney W. Grimes {
138df8bae1dSRodney W. Grimes 	boolean_t rv;
139df8bae1dSRodney W. Grimes 	vm_offset_t saddr, eaddr;
14002c58685SPoul-Henning Kamp 	vm_prot_t prot;
141df8bae1dSRodney W. Grimes 
142e50f5c2eSBruce Evans 	KASSERT((rw & ~VM_PROT_ALL) == 0,
14302c58685SPoul-Henning Kamp 	    ("illegal ``rw'' argument to kernacc (%x)\n", rw));
14402c58685SPoul-Henning Kamp 	prot = rw;
1456cde7a16SDavid Greenman 	saddr = trunc_page((vm_offset_t)addr);
1466cde7a16SDavid Greenman 	eaddr = round_page((vm_offset_t)addr + len);
147d8834602SAlan Cox 	vm_map_lock_read(kernel_map);
148df8bae1dSRodney W. Grimes 	rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot);
149d8834602SAlan Cox 	vm_map_unlock_read(kernel_map);
150df8bae1dSRodney W. Grimes 	return (rv == TRUE);
151df8bae1dSRodney W. Grimes }
152df8bae1dSRodney W. Grimes 
15343a90f3aSAlan Cox /*
15443a90f3aSAlan Cox  * MPSAFE
1552d5c7e45SMatthew Dillon  *
1562d5c7e45SMatthew Dillon  * WARNING!  This code calls vm_map_check_protection() which only checks
1572d5c7e45SMatthew Dillon  * the associated vm_map_entry range.  It does not determine whether the
1582d5c7e45SMatthew Dillon  * contents of the memory is actually readable or writable.  vmapbuf(),
1592d5c7e45SMatthew Dillon  * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be
1602d5c7e45SMatthew Dillon  * used in conjuction with this call.
16143a90f3aSAlan Cox  */
162df8bae1dSRodney W. Grimes int
163df8bae1dSRodney W. Grimes useracc(addr, len, rw)
164c3dfdfd1SAlfred Perlstein 	void *addr;
165df8bae1dSRodney W. Grimes 	int len, rw;
166df8bae1dSRodney W. Grimes {
167df8bae1dSRodney W. Grimes 	boolean_t rv;
16802c58685SPoul-Henning Kamp 	vm_prot_t prot;
16905ba50f5SJake Burkholder 	vm_map_t map;
170df8bae1dSRodney W. Grimes 
171e50f5c2eSBruce Evans 	KASSERT((rw & ~VM_PROT_ALL) == 0,
17202c58685SPoul-Henning Kamp 	    ("illegal ``rw'' argument to useracc (%x)\n", rw));
17302c58685SPoul-Henning Kamp 	prot = rw;
17405ba50f5SJake Burkholder 	map = &curproc->p_vmspace->vm_map;
17505ba50f5SJake Burkholder 	if ((vm_offset_t)addr + len > vm_map_max(map) ||
17605ba50f5SJake Burkholder 	    (vm_offset_t)addr + len < (vm_offset_t)addr) {
17726f9a767SRodney W. Grimes 		return (FALSE);
17826f9a767SRodney W. Grimes 	}
179d8834602SAlan Cox 	vm_map_lock_read(map);
18005ba50f5SJake Burkholder 	rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr),
18105ba50f5SJake Burkholder 	    round_page((vm_offset_t)addr + len), prot);
182d8834602SAlan Cox 	vm_map_unlock_read(map);
183df8bae1dSRodney W. Grimes 	return (rv == TRUE);
184df8bae1dSRodney W. Grimes }
185df8bae1dSRodney W. Grimes 
18643a90f3aSAlan Cox /*
18716929939SDon Lewis  * MPSAFE
18816929939SDon Lewis  */
18916929939SDon Lewis int
190ce8660e3SDon Lewis vslock(addr, len)
191ce8660e3SDon Lewis 	void *addr;
192ce8660e3SDon Lewis 	size_t len;
19316929939SDon Lewis {
19416929939SDon Lewis 	vm_offset_t start, end;
19516929939SDon Lewis 	int error, npages;
19616929939SDon Lewis 
197ce8660e3SDon Lewis 	start = trunc_page((vm_offset_t)addr);
198ce8660e3SDon Lewis 	end = round_page((vm_offset_t)addr + len);
19916929939SDon Lewis 
20016929939SDon Lewis 	/* disable wrap around */
20116929939SDon Lewis 	if (end <= start)
20216929939SDon Lewis 		return (EINVAL);
20316929939SDon Lewis 
20416929939SDon Lewis 	npages = atop(end - start);
20516929939SDon Lewis 
20616929939SDon Lewis 	if (npages > vm_page_max_wired)
20716929939SDon Lewis 		return (ENOMEM);
20816929939SDon Lewis 
209ce8660e3SDon Lewis 	PROC_LOCK(curproc);
210ce8660e3SDon Lewis 	if (npages + pmap_wired_count(vm_map_pmap(&curproc->p_vmspace->vm_map)) >
211ce8660e3SDon Lewis 	    atop(lim_cur(curproc, RLIMIT_MEMLOCK))) {
212ce8660e3SDon Lewis 		PROC_UNLOCK(curproc);
21316929939SDon Lewis 		return (ENOMEM);
21416929939SDon Lewis 	}
215ce8660e3SDon Lewis 	PROC_UNLOCK(curproc);
21616929939SDon Lewis 
21716929939SDon Lewis #if 0
21816929939SDon Lewis 	/*
21916929939SDon Lewis 	 * XXX - not yet
22016929939SDon Lewis 	 *
22116929939SDon Lewis 	 * The limit for transient usage of wired pages should be
22216929939SDon Lewis 	 * larger than for "permanent" wired pages (mlock()).
22316929939SDon Lewis 	 *
22416929939SDon Lewis 	 * Also, the sysctl code, which is the only present user
22516929939SDon Lewis 	 * of vslock(), does a hard loop on EAGAIN.
22616929939SDon Lewis 	 */
22716929939SDon Lewis 	if (npages + cnt.v_wire_count > vm_page_max_wired)
22816929939SDon Lewis 		return (EAGAIN);
22916929939SDon Lewis #endif
23016929939SDon Lewis 
231ce8660e3SDon Lewis 	error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end,
23216929939SDon Lewis 	     VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
23316929939SDon Lewis 
234ce8660e3SDon Lewis 	/*
235ce8660e3SDon Lewis 	 * Return EFAULT on error to match copy{in,out}() behaviour
236ce8660e3SDon Lewis 	 * rather than returning ENOMEM like mlock() would.
237ce8660e3SDon Lewis 	 */
238ce8660e3SDon Lewis 	return (error == KERN_SUCCESS ? 0 : EFAULT);
23916929939SDon Lewis }
24016929939SDon Lewis 
24116929939SDon Lewis /*
24216929939SDon Lewis  * MPSAFE
24316929939SDon Lewis  */
244ce8660e3SDon Lewis void
245ce8660e3SDon Lewis vsunlock(addr, len)
246ce8660e3SDon Lewis 	void *addr;
247ce8660e3SDon Lewis 	size_t len;
24816929939SDon Lewis {
24916929939SDon Lewis 
250ce8660e3SDon Lewis 	/* Rely on the parameter sanity checks performed by vslock(). */
251ce8660e3SDon Lewis 	(void)vm_map_unwire(&curproc->p_vmspace->vm_map,
252ce8660e3SDon Lewis 	    trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len),
25316929939SDon Lewis 	    VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
25416929939SDon Lewis }
25516929939SDon Lewis 
25616929939SDon Lewis /*
257a136efe9SPeter Wemm  * Create the U area for a new process.
258a136efe9SPeter Wemm  * This routine directly affects the fork perf for a process.
259a136efe9SPeter Wemm  */
260a136efe9SPeter Wemm void
261a136efe9SPeter Wemm vm_proc_new(struct proc *p)
262a136efe9SPeter Wemm {
263a136efe9SPeter Wemm 	vm_page_t ma[UAREA_PAGES];
264a136efe9SPeter Wemm 	vm_object_t upobj;
265a136efe9SPeter Wemm 	vm_offset_t up;
266a136efe9SPeter Wemm 	vm_page_t m;
267a136efe9SPeter Wemm 	u_int i;
268a136efe9SPeter Wemm 
269a136efe9SPeter Wemm 	/*
270a136efe9SPeter Wemm 	 * Get a kernel virtual address for the U area for this process.
271a136efe9SPeter Wemm 	 */
272a136efe9SPeter Wemm 	up = kmem_alloc_nofault(kernel_map, UAREA_PAGES * PAGE_SIZE);
273a136efe9SPeter Wemm 	if (up == 0)
274a136efe9SPeter Wemm 		panic("vm_proc_new: upage allocation failed");
275a136efe9SPeter Wemm 	p->p_uarea = (struct user *)up;
276a136efe9SPeter Wemm 
277a136efe9SPeter Wemm 	/*
278ef13663bSAlan Cox 	 * Allocate object and page(s) for the U area.
279a136efe9SPeter Wemm 	 */
280ef13663bSAlan Cox 	upobj = vm_object_allocate(OBJT_DEFAULT, UAREA_PAGES);
281ef13663bSAlan Cox 	p->p_upages_obj = upobj;
282ef13663bSAlan Cox 	VM_OBJECT_LOCK(upobj);
283ef13663bSAlan Cox 	for (i = 0; i < UAREA_PAGES; i++) {
28414f8ceaaSAlan Cox 		m = vm_page_grab(upobj, i,
28514f8ceaaSAlan Cox 		    VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED);
286a136efe9SPeter Wemm 		ma[i] = m;
287a136efe9SPeter Wemm 
288dc907f66SAlan Cox 		vm_page_lock_queues();
289a136efe9SPeter Wemm 		vm_page_wakeup(m);
290a136efe9SPeter Wemm 		m->valid = VM_PAGE_BITS_ALL;
291dc907f66SAlan Cox 		vm_page_unlock_queues();
292a136efe9SPeter Wemm 	}
293ef13663bSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
294a136efe9SPeter Wemm 
295a136efe9SPeter Wemm 	/*
296a136efe9SPeter Wemm 	 * Enter the pages into the kernel address space.
297a136efe9SPeter Wemm 	 */
298a136efe9SPeter Wemm 	pmap_qenter(up, ma, UAREA_PAGES);
299a136efe9SPeter Wemm }
300a136efe9SPeter Wemm 
301a136efe9SPeter Wemm /*
302a136efe9SPeter Wemm  * Dispose the U area for a process that has exited.
303a136efe9SPeter Wemm  * This routine directly impacts the exit perf of a process.
304a136efe9SPeter Wemm  * XXX proc_zone is marked UMA_ZONE_NOFREE, so this should never be called.
305a136efe9SPeter Wemm  */
306a136efe9SPeter Wemm void
307a136efe9SPeter Wemm vm_proc_dispose(struct proc *p)
308a136efe9SPeter Wemm {
309a136efe9SPeter Wemm 	vm_object_t upobj;
310a136efe9SPeter Wemm 	vm_offset_t up;
311a136efe9SPeter Wemm 	vm_page_t m;
312a136efe9SPeter Wemm 
313a136efe9SPeter Wemm 	upobj = p->p_upages_obj;
3146a07e90dSAlan Cox 	VM_OBJECT_LOCK(upobj);
315f59685a4SPeter Wemm 	if (upobj->resident_page_count != UAREA_PAGES)
316f59685a4SPeter Wemm 		panic("vm_proc_dispose: incorrect number of pages in upobj");
3172d09a6adSAlan Cox 	vm_page_lock_queues();
318f59685a4SPeter Wemm 	while ((m = TAILQ_FIRST(&upobj->memq)) != NULL) {
319a136efe9SPeter Wemm 		vm_page_busy(m);
320a136efe9SPeter Wemm 		vm_page_unwire(m, 0);
321a136efe9SPeter Wemm 		vm_page_free(m);
322a136efe9SPeter Wemm 	}
3232d09a6adSAlan Cox 	vm_page_unlock_queues();
3246a07e90dSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
325f59685a4SPeter Wemm 	up = (vm_offset_t)p->p_uarea;
326a136efe9SPeter Wemm 	pmap_qremove(up, UAREA_PAGES);
327a136efe9SPeter Wemm 	kmem_free(kernel_map, up, UAREA_PAGES * PAGE_SIZE);
328a136efe9SPeter Wemm 	vm_object_deallocate(upobj);
329a136efe9SPeter Wemm }
330a136efe9SPeter Wemm 
331a136efe9SPeter Wemm #ifndef NO_SWAPPING
332a136efe9SPeter Wemm /*
333a136efe9SPeter Wemm  * Allow the U area for a process to be prejudicially paged out.
334a136efe9SPeter Wemm  */
33537c84183SPoul-Henning Kamp static void
336a136efe9SPeter Wemm vm_proc_swapout(struct proc *p)
337a136efe9SPeter Wemm {
338a136efe9SPeter Wemm 	vm_object_t upobj;
339a136efe9SPeter Wemm 	vm_offset_t up;
340a136efe9SPeter Wemm 	vm_page_t m;
341a136efe9SPeter Wemm 
342a136efe9SPeter Wemm 	upobj = p->p_upages_obj;
3436a07e90dSAlan Cox 	VM_OBJECT_LOCK(upobj);
344f59685a4SPeter Wemm 	if (upobj->resident_page_count != UAREA_PAGES)
345f59685a4SPeter Wemm 		panic("vm_proc_dispose: incorrect number of pages in upobj");
3462d09a6adSAlan Cox 	vm_page_lock_queues();
347f59685a4SPeter Wemm 	TAILQ_FOREACH(m, &upobj->memq, listq) {
348a136efe9SPeter Wemm 		vm_page_dirty(m);
349a136efe9SPeter Wemm 		vm_page_unwire(m, 0);
350a136efe9SPeter Wemm 	}
3512d09a6adSAlan Cox 	vm_page_unlock_queues();
3526a07e90dSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
353f59685a4SPeter Wemm 	up = (vm_offset_t)p->p_uarea;
354a136efe9SPeter Wemm 	pmap_qremove(up, UAREA_PAGES);
355a136efe9SPeter Wemm }
356a136efe9SPeter Wemm 
357a136efe9SPeter Wemm /*
358a136efe9SPeter Wemm  * Bring the U area for a specified process back in.
359a136efe9SPeter Wemm  */
36037c84183SPoul-Henning Kamp static void
361a136efe9SPeter Wemm vm_proc_swapin(struct proc *p)
362a136efe9SPeter Wemm {
363a136efe9SPeter Wemm 	vm_page_t ma[UAREA_PAGES];
364a136efe9SPeter Wemm 	vm_object_t upobj;
365a136efe9SPeter Wemm 	vm_offset_t up;
366a136efe9SPeter Wemm 	vm_page_t m;
367a136efe9SPeter Wemm 	int rv;
368a136efe9SPeter Wemm 	int i;
369a136efe9SPeter Wemm 
370a136efe9SPeter Wemm 	upobj = p->p_upages_obj;
3718630c117SAlan Cox 	VM_OBJECT_LOCK(upobj);
372a136efe9SPeter Wemm 	for (i = 0; i < UAREA_PAGES; i++) {
373a136efe9SPeter Wemm 		m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
374a136efe9SPeter Wemm 		if (m->valid != VM_PAGE_BITS_ALL) {
375a136efe9SPeter Wemm 			rv = vm_pager_get_pages(upobj, &m, 1, 0);
376a136efe9SPeter Wemm 			if (rv != VM_PAGER_OK)
377a136efe9SPeter Wemm 				panic("vm_proc_swapin: cannot get upage");
378a136efe9SPeter Wemm 		}
379a136efe9SPeter Wemm 		ma[i] = m;
380a7e9138eSPeter Wemm 	}
381a7e9138eSPeter Wemm 	if (upobj->resident_page_count != UAREA_PAGES)
382a7e9138eSPeter Wemm 		panic("vm_proc_swapin: lost pages from upobj");
383e16cfdbeSAlan Cox 	vm_page_lock_queues();
384a7e9138eSPeter Wemm 	TAILQ_FOREACH(m, &upobj->memq, listq) {
385a7e9138eSPeter Wemm 		m->valid = VM_PAGE_BITS_ALL;
386a136efe9SPeter Wemm 		vm_page_wire(m);
387a136efe9SPeter Wemm 		vm_page_wakeup(m);
388a136efe9SPeter Wemm 	}
389e16cfdbeSAlan Cox 	vm_page_unlock_queues();
3906a07e90dSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
391f59685a4SPeter Wemm 	up = (vm_offset_t)p->p_uarea;
392a136efe9SPeter Wemm 	pmap_qenter(up, ma, UAREA_PAGES);
393a136efe9SPeter Wemm }
39492da00bbSMatthew Dillon 
39592da00bbSMatthew Dillon /*
39692da00bbSMatthew Dillon  * Swap in the UAREAs of all processes swapped out to the given device.
39792da00bbSMatthew Dillon  * The pages in the UAREA are marked dirty and their swap metadata is freed.
39892da00bbSMatthew Dillon  */
39992da00bbSMatthew Dillon void
4008f60c087SPoul-Henning Kamp vm_proc_swapin_all(struct swdevt *devidx)
40192da00bbSMatthew Dillon {
40292da00bbSMatthew Dillon 	struct proc *p;
40392da00bbSMatthew Dillon 	vm_object_t object;
40492da00bbSMatthew Dillon 	vm_page_t m;
40592da00bbSMatthew Dillon 
40692da00bbSMatthew Dillon retry:
40792da00bbSMatthew Dillon 	sx_slock(&allproc_lock);
40892da00bbSMatthew Dillon 	FOREACH_PROC_IN_SYSTEM(p) {
40992da00bbSMatthew Dillon 		PROC_LOCK(p);
41092da00bbSMatthew Dillon 		object = p->p_upages_obj;
41117cd3642SAlan Cox 		if (object != NULL) {
41217cd3642SAlan Cox 			VM_OBJECT_LOCK(object);
41317cd3642SAlan Cox 			if (swap_pager_isswapped(object, devidx)) {
41417cd3642SAlan Cox 				VM_OBJECT_UNLOCK(object);
41592da00bbSMatthew Dillon 				sx_sunlock(&allproc_lock);
41692da00bbSMatthew Dillon 				faultin(p);
41792da00bbSMatthew Dillon 				PROC_UNLOCK(p);
4186a07e90dSAlan Cox 				VM_OBJECT_LOCK(object);
41992da00bbSMatthew Dillon 				vm_page_lock_queues();
42092da00bbSMatthew Dillon 				TAILQ_FOREACH(m, &object->memq, listq)
42192da00bbSMatthew Dillon 					vm_page_dirty(m);
42292da00bbSMatthew Dillon 				vm_page_unlock_queues();
42392da00bbSMatthew Dillon 				swap_pager_freespace(object, 0,
42492da00bbSMatthew Dillon 				    object->un_pager.swp.swp_bcount);
4256a07e90dSAlan Cox 				VM_OBJECT_UNLOCK(object);
42692da00bbSMatthew Dillon 				goto retry;
42792da00bbSMatthew Dillon 			}
42817cd3642SAlan Cox 			VM_OBJECT_UNLOCK(object);
42917cd3642SAlan Cox 		}
43092da00bbSMatthew Dillon 		PROC_UNLOCK(p);
43192da00bbSMatthew Dillon 	}
43292da00bbSMatthew Dillon 	sx_sunlock(&allproc_lock);
43392da00bbSMatthew Dillon }
434a136efe9SPeter Wemm #endif
435a136efe9SPeter Wemm 
43649a2507bSAlan Cox #ifndef KSTACK_MAX_PAGES
43749a2507bSAlan Cox #define KSTACK_MAX_PAGES 32
43849a2507bSAlan Cox #endif
43949a2507bSAlan Cox 
44049a2507bSAlan Cox /*
44149a2507bSAlan Cox  * Create the kernel stack (including pcb for i386) for a new thread.
44249a2507bSAlan Cox  * This routine directly affects the fork perf for a process and
44349a2507bSAlan Cox  * create performance for a thread.
44449a2507bSAlan Cox  */
44549a2507bSAlan Cox void
44649a2507bSAlan Cox vm_thread_new(struct thread *td, int pages)
44749a2507bSAlan Cox {
44849a2507bSAlan Cox 	vm_object_t ksobj;
44949a2507bSAlan Cox 	vm_offset_t ks;
45049a2507bSAlan Cox 	vm_page_t m, ma[KSTACK_MAX_PAGES];
45149a2507bSAlan Cox 	int i;
45249a2507bSAlan Cox 
45349a2507bSAlan Cox 	/* Bounds check */
45449a2507bSAlan Cox 	if (pages <= 1)
45549a2507bSAlan Cox 		pages = KSTACK_PAGES;
45649a2507bSAlan Cox 	else if (pages > KSTACK_MAX_PAGES)
45749a2507bSAlan Cox 		pages = KSTACK_MAX_PAGES;
45849a2507bSAlan Cox 	/*
45949a2507bSAlan Cox 	 * Allocate an object for the kstack.
46049a2507bSAlan Cox 	 */
46149a2507bSAlan Cox 	ksobj = vm_object_allocate(OBJT_DEFAULT, pages);
46249a2507bSAlan Cox 	td->td_kstack_obj = ksobj;
46349a2507bSAlan Cox 	/*
46449a2507bSAlan Cox 	 * Get a kernel virtual address for this thread's kstack.
46549a2507bSAlan Cox 	 */
46649a2507bSAlan Cox 	ks = kmem_alloc_nofault(kernel_map,
46749a2507bSAlan Cox 	   (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
46849a2507bSAlan Cox 	if (ks == 0)
46949a2507bSAlan Cox 		panic("vm_thread_new: kstack allocation failed");
47049a2507bSAlan Cox 	if (KSTACK_GUARD_PAGES != 0) {
47149a2507bSAlan Cox 		pmap_qremove(ks, KSTACK_GUARD_PAGES);
47249a2507bSAlan Cox 		ks += KSTACK_GUARD_PAGES * PAGE_SIZE;
47349a2507bSAlan Cox 	}
47449a2507bSAlan Cox 	td->td_kstack = ks;
47549a2507bSAlan Cox 	/*
47649a2507bSAlan Cox 	 * Knowing the number of pages allocated is useful when you
47749a2507bSAlan Cox 	 * want to deallocate them.
47849a2507bSAlan Cox 	 */
47949a2507bSAlan Cox 	td->td_kstack_pages = pages;
48049a2507bSAlan Cox 	/*
48149a2507bSAlan Cox 	 * For the length of the stack, link in a real page of ram for each
48249a2507bSAlan Cox 	 * page of stack.
48349a2507bSAlan Cox 	 */
48449a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
48549a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
48649a2507bSAlan Cox 		/*
48749a2507bSAlan Cox 		 * Get a kernel stack page.
48849a2507bSAlan Cox 		 */
48949a2507bSAlan Cox 		m = vm_page_grab(ksobj, i,
49049a2507bSAlan Cox 		    VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED);
49149a2507bSAlan Cox 		ma[i] = m;
49249a2507bSAlan Cox 		vm_page_lock_queues();
49349a2507bSAlan Cox 		vm_page_wakeup(m);
49449a2507bSAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
49549a2507bSAlan Cox 		vm_page_unlock_queues();
49649a2507bSAlan Cox 	}
49749a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
49849a2507bSAlan Cox 	pmap_qenter(ks, ma, pages);
49949a2507bSAlan Cox }
50049a2507bSAlan Cox 
50149a2507bSAlan Cox /*
50249a2507bSAlan Cox  * Dispose of a thread's kernel stack.
50349a2507bSAlan Cox  */
50449a2507bSAlan Cox void
50549a2507bSAlan Cox vm_thread_dispose(struct thread *td)
50649a2507bSAlan Cox {
50749a2507bSAlan Cox 	vm_object_t ksobj;
50849a2507bSAlan Cox 	vm_offset_t ks;
50949a2507bSAlan Cox 	vm_page_t m;
51049a2507bSAlan Cox 	int i, pages;
51149a2507bSAlan Cox 
51249a2507bSAlan Cox 	pages = td->td_kstack_pages;
51349a2507bSAlan Cox 	ksobj = td->td_kstack_obj;
51449a2507bSAlan Cox 	ks = td->td_kstack;
51549a2507bSAlan Cox 	pmap_qremove(ks, pages);
51649a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
51749a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
51849a2507bSAlan Cox 		m = vm_page_lookup(ksobj, i);
51949a2507bSAlan Cox 		if (m == NULL)
52049a2507bSAlan Cox 			panic("vm_thread_dispose: kstack already missing?");
52149a2507bSAlan Cox 		vm_page_lock_queues();
52249a2507bSAlan Cox 		vm_page_busy(m);
52349a2507bSAlan Cox 		vm_page_unwire(m, 0);
52449a2507bSAlan Cox 		vm_page_free(m);
52549a2507bSAlan Cox 		vm_page_unlock_queues();
52649a2507bSAlan Cox 	}
52749a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
52849a2507bSAlan Cox 	vm_object_deallocate(ksobj);
52949a2507bSAlan Cox 	kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE),
53049a2507bSAlan Cox 	    (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
53149a2507bSAlan Cox }
53249a2507bSAlan Cox 
53349a2507bSAlan Cox /*
53449a2507bSAlan Cox  * Allow a thread's kernel stack to be paged out.
53549a2507bSAlan Cox  */
53649a2507bSAlan Cox void
53749a2507bSAlan Cox vm_thread_swapout(struct thread *td)
53849a2507bSAlan Cox {
53949a2507bSAlan Cox 	vm_object_t ksobj;
54049a2507bSAlan Cox 	vm_page_t m;
54149a2507bSAlan Cox 	int i, pages;
54249a2507bSAlan Cox 
543710338e9SMarcel Moolenaar 	cpu_thread_swapout(td);
54449a2507bSAlan Cox 	pages = td->td_kstack_pages;
54549a2507bSAlan Cox 	ksobj = td->td_kstack_obj;
54649a2507bSAlan Cox 	pmap_qremove(td->td_kstack, pages);
54749a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
54849a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
54949a2507bSAlan Cox 		m = vm_page_lookup(ksobj, i);
55049a2507bSAlan Cox 		if (m == NULL)
55149a2507bSAlan Cox 			panic("vm_thread_swapout: kstack already missing?");
55249a2507bSAlan Cox 		vm_page_lock_queues();
55349a2507bSAlan Cox 		vm_page_dirty(m);
55449a2507bSAlan Cox 		vm_page_unwire(m, 0);
55549a2507bSAlan Cox 		vm_page_unlock_queues();
55649a2507bSAlan Cox 	}
55749a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
55849a2507bSAlan Cox }
55949a2507bSAlan Cox 
56049a2507bSAlan Cox /*
56149a2507bSAlan Cox  * Bring the kernel stack for a specified thread back in.
56249a2507bSAlan Cox  */
56349a2507bSAlan Cox void
56449a2507bSAlan Cox vm_thread_swapin(struct thread *td)
56549a2507bSAlan Cox {
56649a2507bSAlan Cox 	vm_object_t ksobj;
56749a2507bSAlan Cox 	vm_page_t m, ma[KSTACK_MAX_PAGES];
56849a2507bSAlan Cox 	int i, pages, rv;
56949a2507bSAlan Cox 
57049a2507bSAlan Cox 	pages = td->td_kstack_pages;
57149a2507bSAlan Cox 	ksobj = td->td_kstack_obj;
57249a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
57349a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
57449a2507bSAlan Cox 		m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
57549a2507bSAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
57649a2507bSAlan Cox 			rv = vm_pager_get_pages(ksobj, &m, 1, 0);
57749a2507bSAlan Cox 			if (rv != VM_PAGER_OK)
57849a2507bSAlan Cox 				panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid);
57949a2507bSAlan Cox 			m = vm_page_lookup(ksobj, i);
58049a2507bSAlan Cox 			m->valid = VM_PAGE_BITS_ALL;
58149a2507bSAlan Cox 		}
58249a2507bSAlan Cox 		ma[i] = m;
58349a2507bSAlan Cox 		vm_page_lock_queues();
58449a2507bSAlan Cox 		vm_page_wire(m);
58549a2507bSAlan Cox 		vm_page_wakeup(m);
58649a2507bSAlan Cox 		vm_page_unlock_queues();
58749a2507bSAlan Cox 	}
58849a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
58949a2507bSAlan Cox 	pmap_qenter(td->td_kstack, ma, pages);
590710338e9SMarcel Moolenaar 	cpu_thread_swapin(td);
59149a2507bSAlan Cox }
59249a2507bSAlan Cox 
593a136efe9SPeter Wemm /*
59489f4fca2SAlan Cox  * Set up a variable-sized alternate kstack.
59589f4fca2SAlan Cox  */
59689f4fca2SAlan Cox void
59789f4fca2SAlan Cox vm_thread_new_altkstack(struct thread *td, int pages)
59889f4fca2SAlan Cox {
59989f4fca2SAlan Cox 
60089f4fca2SAlan Cox 	td->td_altkstack = td->td_kstack;
60189f4fca2SAlan Cox 	td->td_altkstack_obj = td->td_kstack_obj;
60289f4fca2SAlan Cox 	td->td_altkstack_pages = td->td_kstack_pages;
60389f4fca2SAlan Cox 
60449a2507bSAlan Cox 	vm_thread_new(td, pages);
60589f4fca2SAlan Cox }
60689f4fca2SAlan Cox 
60789f4fca2SAlan Cox /*
60889f4fca2SAlan Cox  * Restore the original kstack.
60989f4fca2SAlan Cox  */
61089f4fca2SAlan Cox void
61189f4fca2SAlan Cox vm_thread_dispose_altkstack(struct thread *td)
61289f4fca2SAlan Cox {
61389f4fca2SAlan Cox 
61449a2507bSAlan Cox 	vm_thread_dispose(td);
61589f4fca2SAlan Cox 
61689f4fca2SAlan Cox 	td->td_kstack = td->td_altkstack;
61789f4fca2SAlan Cox 	td->td_kstack_obj = td->td_altkstack_obj;
61889f4fca2SAlan Cox 	td->td_kstack_pages = td->td_altkstack_pages;
61989f4fca2SAlan Cox 	td->td_altkstack = 0;
62089f4fca2SAlan Cox 	td->td_altkstack_obj = NULL;
62189f4fca2SAlan Cox 	td->td_altkstack_pages = 0;
62289f4fca2SAlan Cox }
62389f4fca2SAlan Cox 
62489f4fca2SAlan Cox /*
625df8bae1dSRodney W. Grimes  * Implement fork's actions on an address space.
626df8bae1dSRodney W. Grimes  * Here we arrange for the address space to be copied or referenced,
627df8bae1dSRodney W. Grimes  * allocate a user struct (pcb and kernel stack), then call the
628df8bae1dSRodney W. Grimes  * machine-dependent layer to fill those in and make the new process
629a2a1c95cSPeter Wemm  * ready to run.  The new process is set up so that it returns directly
630a2a1c95cSPeter Wemm  * to user mode to avoid stack copying and relocation problems.
631df8bae1dSRodney W. Grimes  */
632a2a1c95cSPeter Wemm void
633079b7badSJulian Elischer vm_forkproc(td, p2, td2, flags)
634b40ce416SJulian Elischer 	struct thread *td;
635b40ce416SJulian Elischer 	struct proc *p2;
636079b7badSJulian Elischer 	struct thread *td2;
637a2a1c95cSPeter Wemm 	int flags;
638df8bae1dSRodney W. Grimes {
639b40ce416SJulian Elischer 	struct proc *p1 = td->td_proc;
64054d92145SMatthew Dillon 	struct user *up;
641df8bae1dSRodney W. Grimes 
6420cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6430cddd8f0SMatthew Dillon 
64491c28bfdSLuoqi Chen 	if ((flags & RFPROC) == 0) {
64591c28bfdSLuoqi Chen 		/*
64691c28bfdSLuoqi Chen 		 * Divorce the memory, if it is shared, essentially
64791c28bfdSLuoqi Chen 		 * this changes shared memory amongst threads, into
64891c28bfdSLuoqi Chen 		 * COW locally.
64991c28bfdSLuoqi Chen 		 */
65091c28bfdSLuoqi Chen 		if ((flags & RFMEM) == 0) {
65191c28bfdSLuoqi Chen 			if (p1->p_vmspace->vm_refcnt > 1) {
65291c28bfdSLuoqi Chen 				vmspace_unshare(p1);
65391c28bfdSLuoqi Chen 			}
65491c28bfdSLuoqi Chen 		}
655079b7badSJulian Elischer 		cpu_fork(td, p2, td2, flags);
65691c28bfdSLuoqi Chen 		return;
65791c28bfdSLuoqi Chen 	}
65891c28bfdSLuoqi Chen 
6595856e12eSJohn Dyson 	if (flags & RFMEM) {
6605856e12eSJohn Dyson 		p2->p_vmspace = p1->p_vmspace;
6615856e12eSJohn Dyson 		p1->p_vmspace->vm_refcnt++;
6625856e12eSJohn Dyson 	}
6635856e12eSJohn Dyson 
66490ecac61SMatthew Dillon 	while (vm_page_count_severe()) {
66526f9a767SRodney W. Grimes 		VM_WAIT;
6660d94caffSDavid Greenman 	}
66726f9a767SRodney W. Grimes 
6685856e12eSJohn Dyson 	if ((flags & RFMEM) == 0) {
669df8bae1dSRodney W. Grimes 		p2->p_vmspace = vmspace_fork(p1->p_vmspace);
670df8bae1dSRodney W. Grimes 		if (p1->p_vmspace->vm_shm)
671dabee6feSPeter Wemm 			shmfork(p1, p2);
672a2a1c95cSPeter Wemm 	}
673df8bae1dSRodney W. Grimes 
674b40ce416SJulian Elischer 	/* XXXKSE this is unsatisfactory but should be adequate */
675b40ce416SJulian Elischer 	up = p2->p_uarea;
67690af4afaSJohn Baldwin 	MPASS(p2->p_sigacts != NULL);
677df8bae1dSRodney W. Grimes 
67839fb8e6bSJulian Elischer 	/*
67939fb8e6bSJulian Elischer 	 * p_stats currently points at fields in the user struct
68039fb8e6bSJulian Elischer 	 * but not at &u, instead at p_addr. Copy parts of
68139fb8e6bSJulian Elischer 	 * p_stats; zero the rest of p_stats (statistics).
68239fb8e6bSJulian Elischer 	 */
68339fb8e6bSJulian Elischer 	p2->p_stats = &up->u_stats;
684df8bae1dSRodney W. Grimes 	bzero(&up->u_stats.pstat_startzero,
685df8bae1dSRodney W. Grimes 	    (unsigned) ((caddr_t) &up->u_stats.pstat_endzero -
686df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startzero));
687df8bae1dSRodney W. Grimes 	bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy,
688df8bae1dSRodney W. Grimes 	    ((caddr_t) &up->u_stats.pstat_endcopy -
689df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startcopy));
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes 	/*
692a2a1c95cSPeter Wemm 	 * cpu_fork will copy and update the pcb, set up the kernel stack,
693a2a1c95cSPeter Wemm 	 * and make the child ready to run.
694df8bae1dSRodney W. Grimes 	 */
695079b7badSJulian Elischer 	cpu_fork(td, p2, td2, flags);
696df8bae1dSRodney W. Grimes }
697df8bae1dSRodney W. Grimes 
698df8bae1dSRodney W. Grimes /*
699eb30c1c0SPeter Wemm  * Called after process has been wait(2)'ed apon and is being reaped.
700eb30c1c0SPeter Wemm  * The idea is to reclaim resources that we could not reclaim while
701eb30c1c0SPeter Wemm  * the process was still executing.
702eb30c1c0SPeter Wemm  */
703eb30c1c0SPeter Wemm void
704eb30c1c0SPeter Wemm vm_waitproc(p)
705eb30c1c0SPeter Wemm 	struct proc *p;
706eb30c1c0SPeter Wemm {
707eb30c1c0SPeter Wemm 
708eb30c1c0SPeter Wemm 	GIANT_REQUIRED;
709582ec34cSAlfred Perlstein 	vmspace_exitfree(p);		/* and clean-out the vmspace */
710eb30c1c0SPeter Wemm }
711eb30c1c0SPeter Wemm 
712eb30c1c0SPeter Wemm /*
713df8bae1dSRodney W. Grimes  * Set default limits for VM system.
714df8bae1dSRodney W. Grimes  * Called for proc 0, and then inherited by all others.
7152b14f991SJulian Elischer  *
7162b14f991SJulian Elischer  * XXX should probably act directly on proc0.
717df8bae1dSRodney W. Grimes  */
7182b14f991SJulian Elischer static void
7192b14f991SJulian Elischer vm_init_limits(udata)
7204590fd3aSDavid Greenman 	void *udata;
721df8bae1dSRodney W. Grimes {
72254d92145SMatthew Dillon 	struct proc *p = udata;
72391d5354aSJohn Baldwin 	struct plimit *limp;
724bbc0ec52SDavid Greenman 	int rss_limit;
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes 	/*
7270d94caffSDavid Greenman 	 * Set up the initial limits on process VM. Set the maximum resident
7280d94caffSDavid Greenman 	 * set size to be half of (reasonably) available memory.  Since this
7290d94caffSDavid Greenman 	 * is a soft limit, it comes into effect only when the system is out
7300d94caffSDavid Greenman 	 * of memory - half of main memory helps to favor smaller processes,
731bbc0ec52SDavid Greenman 	 * and reduces thrashing of the object cache.
732df8bae1dSRodney W. Grimes 	 */
73391d5354aSJohn Baldwin 	limp = p->p_limit;
73491d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz;
73591d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz;
73691d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
73791d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
738dd0bd066SDavid Greenman 	/* limit the limit to no less than 2MB */
739f2daac0cSDavid Greenman 	rss_limit = max(cnt.v_free_count, 512);
74091d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
74191d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
742df8bae1dSRodney W. Grimes }
743df8bae1dSRodney W. Grimes 
74426f9a767SRodney W. Grimes void
74526f9a767SRodney W. Grimes faultin(p)
74626f9a767SRodney W. Grimes 	struct proc *p;
74726f9a767SRodney W. Grimes {
74811edc1e0SJohn Baldwin #ifdef NO_SWAPPING
74911edc1e0SJohn Baldwin 
75011edc1e0SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
75111edc1e0SJohn Baldwin 	if ((p->p_sflag & PS_INMEM) == 0)
75211edc1e0SJohn Baldwin 		panic("faultin: proc swapped out with NO_SWAPPING!");
75311edc1e0SJohn Baldwin #else /* !NO_SWAPPING */
754664f718bSJohn Baldwin 	struct thread *td;
75526f9a767SRodney W. Grimes 
756a136efe9SPeter Wemm 	GIANT_REQUIRED;
757c96d52a9SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
7581d7b9ed2SJulian Elischer 	/*
7591d7b9ed2SJulian Elischer 	 * If another process is swapping in this process,
7601d7b9ed2SJulian Elischer 	 * just wait until it finishes.
7611d7b9ed2SJulian Elischer 	 */
762664f718bSJohn Baldwin 	if (p->p_sflag & PS_SWAPPINGIN)
7631d7b9ed2SJulian Elischer 		msleep(&p->p_sflag, &p->p_mtx, PVM, "faultin", 0);
764664f718bSJohn Baldwin 	else if ((p->p_sflag & PS_INMEM) == 0) {
765664f718bSJohn Baldwin 		/*
766664f718bSJohn Baldwin 		 * Don't let another thread swap process p out while we are
767664f718bSJohn Baldwin 		 * busy swapping it in.
768664f718bSJohn Baldwin 		 */
769664f718bSJohn Baldwin 		++p->p_lock;
7701d7b9ed2SJulian Elischer 		mtx_lock_spin(&sched_lock);
7711d7b9ed2SJulian Elischer 		p->p_sflag |= PS_SWAPPINGIN;
7729ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
77345ece682SJohn Baldwin 		PROC_UNLOCK(p);
77426f9a767SRodney W. Grimes 
775a136efe9SPeter Wemm 		vm_proc_swapin(p);
776664f718bSJohn Baldwin 		FOREACH_THREAD_IN_PROC(p, td)
77749a2507bSAlan Cox 			vm_thread_swapin(td);
77826f9a767SRodney W. Grimes 
77945ece682SJohn Baldwin 		PROC_LOCK(p);
7809ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
7819eb881f8SSeigo Tanimura 		p->p_sflag &= ~PS_SWAPPINGIN;
7829eb881f8SSeigo Tanimura 		p->p_sflag |= PS_INMEM;
783664f718bSJohn Baldwin 		FOREACH_THREAD_IN_PROC(p, td) {
784664f718bSJohn Baldwin 			TD_CLR_SWAPPED(td);
78571fad9fdSJulian Elischer 			if (TD_CAN_RUN(td))
78671fad9fdSJulian Elischer 				setrunnable(td);
787664f718bSJohn Baldwin 		}
788664f718bSJohn Baldwin 		mtx_unlock_spin(&sched_lock);
78926f9a767SRodney W. Grimes 
7901d7b9ed2SJulian Elischer 		wakeup(&p->p_sflag);
79126f9a767SRodney W. Grimes 
792664f718bSJohn Baldwin 		/* Allow other threads to swap p out now. */
79326f9a767SRodney W. Grimes 		--p->p_lock;
79426f9a767SRodney W. Grimes 	}
79511edc1e0SJohn Baldwin #endif /* NO_SWAPPING */
79626f9a767SRodney W. Grimes }
79726f9a767SRodney W. Grimes 
798df8bae1dSRodney W. Grimes /*
79926f9a767SRodney W. Grimes  * This swapin algorithm attempts to swap-in processes only if there
80026f9a767SRodney W. Grimes  * is enough space for them.  Of course, if a process waits for a long
80126f9a767SRodney W. Grimes  * time, it will be swapped in anyway.
8020384fff8SJason Evans  *
803e602ba25SJulian Elischer  *  XXXKSE - process with the thread with highest priority counts..
804b40ce416SJulian Elischer  *
8050384fff8SJason Evans  * Giant is still held at this point, to be released in tsleep.
806df8bae1dSRodney W. Grimes  */
8072b14f991SJulian Elischer /* ARGSUSED*/
8082b14f991SJulian Elischer static void
809d841aaa7SBruce Evans scheduler(dummy)
810d841aaa7SBruce Evans 	void *dummy;
811df8bae1dSRodney W. Grimes {
81254d92145SMatthew Dillon 	struct proc *p;
813e602ba25SJulian Elischer 	struct thread *td;
81454d92145SMatthew Dillon 	int pri;
815df8bae1dSRodney W. Grimes 	struct proc *pp;
816df8bae1dSRodney W. Grimes 	int ppri;
817df8bae1dSRodney W. Grimes 
818c96d52a9SJohn Baldwin 	mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED);
8190cddd8f0SMatthew Dillon 	/* GIANT_REQUIRED */
8200384fff8SJason Evans 
821df8bae1dSRodney W. Grimes loop:
82290ecac61SMatthew Dillon 	if (vm_page_count_min()) {
8230d94caffSDavid Greenman 		VM_WAIT;
82490ecac61SMatthew Dillon 		goto loop;
8250d94caffSDavid Greenman 	}
82626f9a767SRodney W. Grimes 
827df8bae1dSRodney W. Grimes 	pp = NULL;
828df8bae1dSRodney W. Grimes 	ppri = INT_MIN;
8291005a129SJohn Baldwin 	sx_slock(&allproc_lock);
830b40ce416SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
831b40ce416SJulian Elischer 		struct ksegrp *kg;
832664f718bSJohn Baldwin 		if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) {
833e602ba25SJulian Elischer 			continue;
834e602ba25SJulian Elischer 		}
8359ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
836e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
8371d7b9ed2SJulian Elischer 			/*
83871fad9fdSJulian Elischer 			 * An otherwise runnable thread of a process
83971fad9fdSJulian Elischer 			 * swapped out has only the TDI_SWAPPED bit set.
84071fad9fdSJulian Elischer 			 *
8411d7b9ed2SJulian Elischer 			 */
84271fad9fdSJulian Elischer 			if (td->td_inhibitors == TDI_SWAPPED) {
843e602ba25SJulian Elischer 				kg = td->td_ksegrp;
844b40ce416SJulian Elischer 				pri = p->p_swtime + kg->kg_slptime;
8455074aecdSJohn Baldwin 				if ((p->p_sflag & PS_SWAPINREQ) == 0) {
846b40ce416SJulian Elischer 					pri -= kg->kg_nice * 8;
847a669a6e9SJohn Dyson 				}
84895461b45SJohn Dyson 
84926f9a767SRodney W. Grimes 				/*
850b40ce416SJulian Elischer 				 * if this ksegrp is higher priority
851b40ce416SJulian Elischer 				 * and there is enough space, then select
852b40ce416SJulian Elischer 				 * this process instead of the previous
853b40ce416SJulian Elischer 				 * selection.
85426f9a767SRodney W. Grimes 				 */
8550d94caffSDavid Greenman 				if (pri > ppri) {
856df8bae1dSRodney W. Grimes 					pp = p;
857df8bae1dSRodney W. Grimes 					ppri = pri;
858df8bae1dSRodney W. Grimes 				}
859df8bae1dSRodney W. Grimes 			}
860b40ce416SJulian Elischer 		}
8619ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
862df8bae1dSRodney W. Grimes 	}
8631005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
86426f9a767SRodney W. Grimes 
865df8bae1dSRodney W. Grimes 	/*
866a669a6e9SJohn Dyson 	 * Nothing to do, back to sleep.
867df8bae1dSRodney W. Grimes 	 */
868df8bae1dSRodney W. Grimes 	if ((p = pp) == NULL) {
869ea754954SJohn Baldwin 		tsleep(&proc0, PVM, "sched", maxslp * hz / 2);
870df8bae1dSRodney W. Grimes 		goto loop;
871df8bae1dSRodney W. Grimes 	}
8721d7b9ed2SJulian Elischer 	PROC_LOCK(p);
8731d7b9ed2SJulian Elischer 
8741d7b9ed2SJulian Elischer 	/*
8751d7b9ed2SJulian Elischer 	 * Another process may be bringing or may have already
8761d7b9ed2SJulian Elischer 	 * brought this process in while we traverse all threads.
8771d7b9ed2SJulian Elischer 	 * Or, this process may even be being swapped out again.
8781d7b9ed2SJulian Elischer 	 */
879664f718bSJohn Baldwin 	if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) {
8801d7b9ed2SJulian Elischer 		PROC_UNLOCK(p);
8811d7b9ed2SJulian Elischer 		goto loop;
8821d7b9ed2SJulian Elischer 	}
8831d7b9ed2SJulian Elischer 
884664f718bSJohn Baldwin 	mtx_lock_spin(&sched_lock);
8851d7b9ed2SJulian Elischer 	p->p_sflag &= ~PS_SWAPINREQ;
886664f718bSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
887a669a6e9SJohn Dyson 
888df8bae1dSRodney W. Grimes 	/*
88926f9a767SRodney W. Grimes 	 * We would like to bring someone in. (only if there is space).
890e602ba25SJulian Elischer 	 * [What checks the space? ]
891df8bae1dSRodney W. Grimes 	 */
89226f9a767SRodney W. Grimes 	faultin(p);
89345ece682SJohn Baldwin 	PROC_UNLOCK(p);
894664f718bSJohn Baldwin 	mtx_lock_spin(&sched_lock);
895df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
8969ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
897df8bae1dSRodney W. Grimes 	goto loop;
898df8bae1dSRodney W. Grimes }
899df8bae1dSRodney W. Grimes 
9005afce282SDavid Greenman #ifndef NO_SWAPPING
9015afce282SDavid Greenman 
902ceb0cf87SJohn Dyson /*
903ceb0cf87SJohn Dyson  * Swap_idle_threshold1 is the guaranteed swapped in time for a process
904ceb0cf87SJohn Dyson  */
905303b270bSEivind Eklund static int swap_idle_threshold1 = 2;
9062a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW,
9079faaf3b3STom Rhodes     &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process");
908ceb0cf87SJohn Dyson 
909ceb0cf87SJohn Dyson /*
910ceb0cf87SJohn Dyson  * Swap_idle_threshold2 is the time that a process can be idle before
911ceb0cf87SJohn Dyson  * it will be swapped out, if idle swapping is enabled.
912ceb0cf87SJohn Dyson  */
913303b270bSEivind Eklund static int swap_idle_threshold2 = 10;
9142a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW,
9159faaf3b3STom Rhodes     &swap_idle_threshold2, 0, "Time before a process will be swapped out");
916ceb0cf87SJohn Dyson 
917df8bae1dSRodney W. Grimes /*
918df8bae1dSRodney W. Grimes  * Swapout is driven by the pageout daemon.  Very simple, we find eligible
919df8bae1dSRodney W. Grimes  * procs and unwire their u-areas.  We try to always "swap" at least one
920df8bae1dSRodney W. Grimes  * process in case we need the room for a swapin.
921df8bae1dSRodney W. Grimes  * If any procs have been sleeping/stopped for at least maxslp seconds,
922df8bae1dSRodney W. Grimes  * they are swapped.  Else, we swap the longest-sleeping or stopped process,
923df8bae1dSRodney W. Grimes  * if any, otherwise the longest-resident process.
924df8bae1dSRodney W. Grimes  */
925df8bae1dSRodney W. Grimes void
9263a2dc656SJohn Dyson swapout_procs(action)
9273a2dc656SJohn Dyson int action;
928df8bae1dSRodney W. Grimes {
92954d92145SMatthew Dillon 	struct proc *p;
930e602ba25SJulian Elischer 	struct thread *td;
931b40ce416SJulian Elischer 	struct ksegrp *kg;
932df8bae1dSRodney W. Grimes 	int didswap = 0;
933df8bae1dSRodney W. Grimes 
9340cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
9350cddd8f0SMatthew Dillon 
9360d94caffSDavid Greenman retry:
9373a2189d4SJohn Baldwin 	sx_slock(&allproc_lock);
938e602ba25SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
939b18bfc3dSJohn Dyson 		struct vmspace *vm;
940b40ce416SJulian Elischer 		int minslptime = 100000;
941b18bfc3dSJohn Dyson 
9429eb881f8SSeigo Tanimura 		/*
943b1f99ebeSSeigo Tanimura 		 * Watch out for a process in
944b1f99ebeSSeigo Tanimura 		 * creation.  It may have no
9451c865ac7SJohn Baldwin 		 * address space or lock yet.
9461c865ac7SJohn Baldwin 		 */
9471c865ac7SJohn Baldwin 		mtx_lock_spin(&sched_lock);
9481c865ac7SJohn Baldwin 		if (p->p_state == PRS_NEW) {
9491c865ac7SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
9501c865ac7SJohn Baldwin 			continue;
9511c865ac7SJohn Baldwin 		}
9521c865ac7SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
9531c865ac7SJohn Baldwin 
9541c865ac7SJohn Baldwin 		/*
955b1f99ebeSSeigo Tanimura 		 * An aio daemon switches its
956b1f99ebeSSeigo Tanimura 		 * address space while running.
957b1f99ebeSSeigo Tanimura 		 * Perform a quick check whether
958b1f99ebeSSeigo Tanimura 		 * a process has P_SYSTEM.
9599eb881f8SSeigo Tanimura 		 */
9608f887403SJohn Baldwin 		if ((p->p_flag & P_SYSTEM) != 0)
961b1f99ebeSSeigo Tanimura 			continue;
9621c865ac7SJohn Baldwin 
9631c865ac7SJohn Baldwin 		/*
9641c865ac7SJohn Baldwin 		 * Do not swapout a process that
9651c865ac7SJohn Baldwin 		 * is waiting for VM data
9661c865ac7SJohn Baldwin 		 * structures as there is a possible
9671c865ac7SJohn Baldwin 		 * deadlock.  Test this first as
9681c865ac7SJohn Baldwin 		 * this may block.
9691c865ac7SJohn Baldwin 		 *
9701c865ac7SJohn Baldwin 		 * Lock the map until swapout
9711c865ac7SJohn Baldwin 		 * finishes, or a thread of this
9721c865ac7SJohn Baldwin 		 * process may attempt to alter
9731c865ac7SJohn Baldwin 		 * the map.
9741c865ac7SJohn Baldwin 		 */
9758f887403SJohn Baldwin 		PROC_LOCK(p);
9769eb881f8SSeigo Tanimura 		vm = p->p_vmspace;
977b1f99ebeSSeigo Tanimura 		KASSERT(vm != NULL,
978b1f99ebeSSeigo Tanimura 			("swapout_procs: a process has no address space"));
9799eb881f8SSeigo Tanimura 		++vm->vm_refcnt;
980b1f99ebeSSeigo Tanimura 		PROC_UNLOCK(p);
9819eb881f8SSeigo Tanimura 		if (!vm_map_trylock(&vm->vm_map))
9829eb881f8SSeigo Tanimura 			goto nextproc1;
9839eb881f8SSeigo Tanimura 
9845074aecdSJohn Baldwin 		PROC_LOCK(p);
98569b40456SJohn Baldwin 		if (p->p_lock != 0 ||
9861279572aSDavid Xu 		    (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT)
9871279572aSDavid Xu 		    ) != 0) {
9889eb881f8SSeigo Tanimura 			goto nextproc2;
9895074aecdSJohn Baldwin 		}
99023955314SAlfred Perlstein 		/*
99123955314SAlfred Perlstein 		 * only aiod changes vmspace, however it will be
99223955314SAlfred Perlstein 		 * skipped because of the if statement above checking
99323955314SAlfred Perlstein 		 * for P_SYSTEM
99423955314SAlfred Perlstein 		 */
995664f718bSJohn Baldwin 		if ((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) != PS_INMEM)
996664f718bSJohn Baldwin 			goto nextproc2;
99769b40456SJohn Baldwin 
998e602ba25SJulian Elischer 		switch (p->p_state) {
9990d94caffSDavid Greenman 		default:
1000e602ba25SJulian Elischer 			/* Don't swap out processes in any sort
1001e602ba25SJulian Elischer 			 * of 'special' state. */
10028f887403SJohn Baldwin 			break;
1003df8bae1dSRodney W. Grimes 
1004e602ba25SJulian Elischer 		case PRS_NORMAL:
10058f887403SJohn Baldwin 			mtx_lock_spin(&sched_lock);
100626f9a767SRodney W. Grimes 			/*
1007bfbfac11SDavid Greenman 			 * do not swapout a realtime process
1008b40ce416SJulian Elischer 			 * Check all the thread groups..
1009bfbfac11SDavid Greenman 			 */
1010b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
10119eb881f8SSeigo Tanimura 				if (PRI_IS_REALTIME(kg->kg_pri_class))
1012b40ce416SJulian Elischer 					goto nextproc;
1013bfbfac11SDavid Greenman 
1014bfbfac11SDavid Greenman 				/*
10159eb881f8SSeigo Tanimura 				 * Guarantee swap_idle_threshold1
1016ceb0cf87SJohn Dyson 				 * time in memory.
10170d94caffSDavid Greenman 				 */
10189eb881f8SSeigo Tanimura 				if (kg->kg_slptime < swap_idle_threshold1)
1019b40ce416SJulian Elischer 					goto nextproc;
10209eb881f8SSeigo Tanimura 
10211d7b9ed2SJulian Elischer 				/*
10229eb881f8SSeigo Tanimura 				 * Do not swapout a process if it is
10239eb881f8SSeigo Tanimura 				 * waiting on a critical event of some
10249eb881f8SSeigo Tanimura 				 * kind or there is a thread whose
10259eb881f8SSeigo Tanimura 				 * pageable memory may be accessed.
10261d7b9ed2SJulian Elischer 				 *
10271d7b9ed2SJulian Elischer 				 * This could be refined to support
10281d7b9ed2SJulian Elischer 				 * swapping out a thread.
10291d7b9ed2SJulian Elischer 				 */
10309eb881f8SSeigo Tanimura 				FOREACH_THREAD_IN_GROUP(kg, td) {
10311d7b9ed2SJulian Elischer 					if ((td->td_priority) < PSOCK ||
10329eb881f8SSeigo Tanimura 					    !thread_safetoswapout(td))
1033e602ba25SJulian Elischer 						goto nextproc;
1034e602ba25SJulian Elischer 				}
1035ceb0cf87SJohn Dyson 				/*
1036b40ce416SJulian Elischer 				 * If the system is under memory stress,
1037b40ce416SJulian Elischer 				 * or if we are swapping
1038b40ce416SJulian Elischer 				 * idle processes >= swap_idle_threshold2,
1039b40ce416SJulian Elischer 				 * then swap the process out.
1040ceb0cf87SJohn Dyson 				 */
1041ceb0cf87SJohn Dyson 				if (((action & VM_SWAP_NORMAL) == 0) &&
1042ceb0cf87SJohn Dyson 				    (((action & VM_SWAP_IDLE) == 0) ||
10439eb881f8SSeigo Tanimura 				    (kg->kg_slptime < swap_idle_threshold2)))
1044b40ce416SJulian Elischer 					goto nextproc;
10459eb881f8SSeigo Tanimura 
1046b40ce416SJulian Elischer 				if (minslptime > kg->kg_slptime)
1047b40ce416SJulian Elischer 					minslptime = kg->kg_slptime;
1048b40ce416SJulian Elischer 			}
10490d94caffSDavid Greenman 
105011b224dcSDavid Greenman 			/*
10510d94caffSDavid Greenman 			 * If the process has been asleep for awhile and had
10520d94caffSDavid Greenman 			 * most of its pages taken away already, swap it out.
105311b224dcSDavid Greenman 			 */
1054ceb0cf87SJohn Dyson 			if ((action & VM_SWAP_NORMAL) ||
1055ceb0cf87SJohn Dyson 				((action & VM_SWAP_IDLE) &&
1056b40ce416SJulian Elischer 				 (minslptime > swap_idle_threshold2))) {
1057df8bae1dSRodney W. Grimes 				swapout(p);
1058df8bae1dSRodney W. Grimes 				didswap++;
10599eb881f8SSeigo Tanimura 				mtx_unlock_spin(&sched_lock);
1060664f718bSJohn Baldwin 				PROC_UNLOCK(p);
10619eb881f8SSeigo Tanimura 				vm_map_unlock(&vm->vm_map);
10629eb881f8SSeigo Tanimura 				vmspace_free(vm);
10639eb881f8SSeigo Tanimura 				sx_sunlock(&allproc_lock);
10640d94caffSDavid Greenman 				goto retry;
1065c96d52a9SJohn Baldwin 			}
1066b40ce416SJulian Elischer nextproc:
10679eb881f8SSeigo Tanimura 			mtx_unlock_spin(&sched_lock);
10688f887403SJohn Baldwin 		}
10699eb881f8SSeigo Tanimura nextproc2:
10709eb881f8SSeigo Tanimura 		PROC_UNLOCK(p);
10719eb881f8SSeigo Tanimura 		vm_map_unlock(&vm->vm_map);
10729eb881f8SSeigo Tanimura nextproc1:
10739eb881f8SSeigo Tanimura 		vmspace_free(vm);
107430171114SPeter Wemm 		continue;
1075ceb0cf87SJohn Dyson 	}
10761005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
107726f9a767SRodney W. Grimes 	/*
107826f9a767SRodney W. Grimes 	 * If we swapped something out, and another process needed memory,
107926f9a767SRodney W. Grimes 	 * then wakeup the sched process.
108026f9a767SRodney W. Grimes 	 */
10810d94caffSDavid Greenman 	if (didswap)
108224a1cce3SDavid Greenman 		wakeup(&proc0);
1083df8bae1dSRodney W. Grimes }
1084df8bae1dSRodney W. Grimes 
1085f708ef1bSPoul-Henning Kamp static void
1086df8bae1dSRodney W. Grimes swapout(p)
108754d92145SMatthew Dillon 	struct proc *p;
1088df8bae1dSRodney W. Grimes {
1089b40ce416SJulian Elischer 	struct thread *td;
1090df8bae1dSRodney W. Grimes 
1091ea754954SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
10929eb881f8SSeigo Tanimura 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1093d3a34985SJohn Dyson #if defined(SWAP_DEBUG)
1094d3a34985SJohn Dyson 	printf("swapping out %d\n", p->p_pid);
1095d3a34985SJohn Dyson #endif
10961d7b9ed2SJulian Elischer 
10971d7b9ed2SJulian Elischer 	/*
10989eb881f8SSeigo Tanimura 	 * The states of this process and its threads may have changed
10999eb881f8SSeigo Tanimura 	 * by now.  Assuming that there is only one pageout daemon thread,
11009eb881f8SSeigo Tanimura 	 * this process should still be in memory.
11019eb881f8SSeigo Tanimura 	 */
1102664f718bSJohn Baldwin 	KASSERT((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) == PS_INMEM,
11039eb881f8SSeigo Tanimura 		("swapout: lost a swapout race?"));
11049eb881f8SSeigo Tanimura 
11059eb881f8SSeigo Tanimura #if defined(INVARIANTS)
11069eb881f8SSeigo Tanimura 	/*
11071d7b9ed2SJulian Elischer 	 * Make sure that all threads are safe to be swapped out.
11081d7b9ed2SJulian Elischer 	 *
11091d7b9ed2SJulian Elischer 	 * Alternatively, we could swap out only safe threads.
11101d7b9ed2SJulian Elischer 	 */
11111d7b9ed2SJulian Elischer 	FOREACH_THREAD_IN_PROC(p, td) {
11129eb881f8SSeigo Tanimura 		KASSERT(thread_safetoswapout(td),
11139eb881f8SSeigo Tanimura 			("swapout: there is a thread not safe for swapout"));
11141d7b9ed2SJulian Elischer 	}
11159eb881f8SSeigo Tanimura #endif /* INVARIANTS */
11161d7b9ed2SJulian Elischer 
111726f9a767SRodney W. Grimes 	++p->p_stats->p_ru.ru_nswap;
1118df8bae1dSRodney W. Grimes 	/*
111926f9a767SRodney W. Grimes 	 * remember the process resident count
1120df8bae1dSRodney W. Grimes 	 */
1121b1028ad1SLuoqi Chen 	p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
1122df8bae1dSRodney W. Grimes 
11239eb881f8SSeigo Tanimura 	p->p_sflag &= ~PS_INMEM;
1124664f718bSJohn Baldwin 	p->p_sflag |= PS_SWAPPINGOUT;
1125664f718bSJohn Baldwin 	PROC_UNLOCK(p);
1126664f718bSJohn Baldwin 	FOREACH_THREAD_IN_PROC(p, td)
1127664f718bSJohn Baldwin 		TD_SET_SWAPPED(td);
11289ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
112926f9a767SRodney W. Grimes 
1130a136efe9SPeter Wemm 	vm_proc_swapout(p);
1131664f718bSJohn Baldwin 	FOREACH_THREAD_IN_PROC(p, td)
113249a2507bSAlan Cox 		vm_thread_swapout(td);
1133664f718bSJohn Baldwin 
1134664f718bSJohn Baldwin 	PROC_LOCK(p);
11359ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1136664f718bSJohn Baldwin 	p->p_sflag &= ~PS_SWAPPINGOUT;
1137df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
1138df8bae1dSRodney W. Grimes }
11395afce282SDavid Greenman #endif /* !NO_SWAPPING */
1140