xref: /freebsd/sys/vm/vm_glue.c (revision d9b2500eeff7483f0219014c55beee1141daaf5d)
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  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_glue.c	8.6 (Berkeley) 1/5/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
39df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
40df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
41df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
42df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
46df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
51df8bae1dSRodney W. Grimes  *  School of Computer Science
52df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
53df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
56df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
57df8bae1dSRodney W. Grimes  */
58df8bae1dSRodney W. Grimes 
59874651b1SDavid E. O'Brien #include <sys/cdefs.h>
60874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
61874651b1SDavid E. O'Brien 
62faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
6315a7ad60SPeter Wemm #include "opt_kstack_pages.h"
6415a7ad60SPeter Wemm #include "opt_kstack_max_pages.h"
65e9822d92SJoerg Wunsch 
66df8bae1dSRodney W. Grimes #include <sys/param.h>
67df8bae1dSRodney W. Grimes #include <sys/systm.h>
68104a9b7eSAlexander Kabaev #include <sys/limits.h>
69fb919e4dSMark Murray #include <sys/lock.h>
70fb919e4dSMark Murray #include <sys/mutex.h>
71df8bae1dSRodney W. Grimes #include <sys/proc.h>
72df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
733aa12267SBruce Evans #include <sys/shm.h>
74efeaf95aSDavid Greenman #include <sys/vmmeter.h>
751005a129SJohn Baldwin #include <sys/sx.h>
76ceb0cf87SJohn Dyson #include <sys/sysctl.h>
77df8bae1dSRodney W. Grimes 
7826f9a767SRodney W. Grimes #include <sys/kernel.h>
790384fff8SJason Evans #include <sys/ktr.h>
80a2a1c95cSPeter Wemm #include <sys/unistd.h>
8126f9a767SRodney W. Grimes 
82df8bae1dSRodney W. Grimes #include <vm/vm.h>
83efeaf95aSDavid Greenman #include <vm/vm_param.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8726f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
88a136efe9SPeter Wemm #include <vm/vm_object.h>
89df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
91a136efe9SPeter Wemm #include <vm/vm_pager.h>
9292da00bbSMatthew Dillon #include <vm/swap_pager.h>
93efeaf95aSDavid Greenman 
94efeaf95aSDavid Greenman #include <sys/user.h>
95df8bae1dSRodney W. Grimes 
96ea754954SJohn Baldwin extern int maxslp;
97ea754954SJohn Baldwin 
982b14f991SJulian Elischer /*
992b14f991SJulian Elischer  * System initialization
1002b14f991SJulian Elischer  *
1012b14f991SJulian Elischer  * Note: proc0 from proc.h
1022b14f991SJulian Elischer  */
10311caded3SAlfred Perlstein static void vm_init_limits(void *);
1044590fd3aSDavid Greenman SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0)
1052b14f991SJulian Elischer 
1062b14f991SJulian Elischer /*
1072b14f991SJulian Elischer  * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
1082b14f991SJulian Elischer  *
1092b14f991SJulian Elischer  * Note: run scheduling should be divorced from the vm system.
1102b14f991SJulian Elischer  */
11111caded3SAlfred Perlstein static void scheduler(void *);
1129a44a82bSBruce Evans SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_ANY, scheduler, NULL)
1132b14f991SJulian Elischer 
114e50f5c2eSBruce Evans #ifndef NO_SWAPPING
11511caded3SAlfred Perlstein static void swapout(struct proc *);
116a136efe9SPeter Wemm static void vm_proc_swapin(struct proc *p);
117a136efe9SPeter Wemm static void vm_proc_swapout(struct proc *p);
118e50f5c2eSBruce Evans #endif
119f708ef1bSPoul-Henning Kamp 
12043a90f3aSAlan Cox /*
12143a90f3aSAlan Cox  * MPSAFE
1222d5c7e45SMatthew Dillon  *
1232d5c7e45SMatthew Dillon  * WARNING!  This code calls vm_map_check_protection() which only checks
1242d5c7e45SMatthew Dillon  * the associated vm_map_entry range.  It does not determine whether the
1252d5c7e45SMatthew Dillon  * contents of the memory is actually readable or writable.  In most cases
1262d5c7e45SMatthew Dillon  * just checking the vm_map_entry is sufficient within the kernel's address
1272d5c7e45SMatthew Dillon  * space.
12843a90f3aSAlan Cox  */
129df8bae1dSRodney W. Grimes int
130df8bae1dSRodney W. Grimes kernacc(addr, len, rw)
131c3dfdfd1SAlfred Perlstein 	void *addr;
132df8bae1dSRodney W. Grimes 	int len, rw;
133df8bae1dSRodney W. Grimes {
134df8bae1dSRodney W. Grimes 	boolean_t rv;
135df8bae1dSRodney W. Grimes 	vm_offset_t saddr, eaddr;
13602c58685SPoul-Henning Kamp 	vm_prot_t prot;
137df8bae1dSRodney W. Grimes 
138e50f5c2eSBruce Evans 	KASSERT((rw & ~VM_PROT_ALL) == 0,
13902c58685SPoul-Henning Kamp 	    ("illegal ``rw'' argument to kernacc (%x)\n", rw));
14002c58685SPoul-Henning Kamp 	prot = rw;
1416cde7a16SDavid Greenman 	saddr = trunc_page((vm_offset_t)addr);
1426cde7a16SDavid Greenman 	eaddr = round_page((vm_offset_t)addr + len);
143d8834602SAlan Cox 	vm_map_lock_read(kernel_map);
144df8bae1dSRodney W. Grimes 	rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot);
145d8834602SAlan Cox 	vm_map_unlock_read(kernel_map);
146df8bae1dSRodney W. Grimes 	return (rv == TRUE);
147df8bae1dSRodney W. Grimes }
148df8bae1dSRodney W. Grimes 
14943a90f3aSAlan Cox /*
15043a90f3aSAlan Cox  * MPSAFE
1512d5c7e45SMatthew Dillon  *
1522d5c7e45SMatthew Dillon  * WARNING!  This code calls vm_map_check_protection() which only checks
1532d5c7e45SMatthew Dillon  * the associated vm_map_entry range.  It does not determine whether the
1542d5c7e45SMatthew Dillon  * contents of the memory is actually readable or writable.  vmapbuf(),
1552d5c7e45SMatthew Dillon  * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be
1562d5c7e45SMatthew Dillon  * used in conjuction with this call.
15743a90f3aSAlan Cox  */
158df8bae1dSRodney W. Grimes int
159df8bae1dSRodney W. Grimes useracc(addr, len, rw)
160c3dfdfd1SAlfred Perlstein 	void *addr;
161df8bae1dSRodney W. Grimes 	int len, rw;
162df8bae1dSRodney W. Grimes {
163df8bae1dSRodney W. Grimes 	boolean_t rv;
16402c58685SPoul-Henning Kamp 	vm_prot_t prot;
16505ba50f5SJake Burkholder 	vm_map_t map;
166df8bae1dSRodney W. Grimes 
167e50f5c2eSBruce Evans 	KASSERT((rw & ~VM_PROT_ALL) == 0,
16802c58685SPoul-Henning Kamp 	    ("illegal ``rw'' argument to useracc (%x)\n", rw));
16902c58685SPoul-Henning Kamp 	prot = rw;
17005ba50f5SJake Burkholder 	map = &curproc->p_vmspace->vm_map;
17105ba50f5SJake Burkholder 	if ((vm_offset_t)addr + len > vm_map_max(map) ||
17205ba50f5SJake Burkholder 	    (vm_offset_t)addr + len < (vm_offset_t)addr) {
17326f9a767SRodney W. Grimes 		return (FALSE);
17426f9a767SRodney W. Grimes 	}
175d8834602SAlan Cox 	vm_map_lock_read(map);
17605ba50f5SJake Burkholder 	rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr),
17705ba50f5SJake Burkholder 	    round_page((vm_offset_t)addr + len), prot);
178d8834602SAlan Cox 	vm_map_unlock_read(map);
179df8bae1dSRodney W. Grimes 	return (rv == TRUE);
180df8bae1dSRodney W. Grimes }
181df8bae1dSRodney W. Grimes 
18216929939SDon Lewis int
183f0ea4612SDon Lewis vslock(void *addr, size_t len)
18416929939SDon Lewis {
185bb734798SDon Lewis 	vm_offset_t end, last, start;
186bb734798SDon Lewis 	vm_size_t npages;
187bb734798SDon Lewis 	int error;
18816929939SDon Lewis 
189bb734798SDon Lewis 	last = (vm_offset_t)addr + len;
190ce8660e3SDon Lewis 	start = trunc_page((vm_offset_t)addr);
191bb734798SDon Lewis 	end = round_page(last);
192bb734798SDon Lewis 	if (last < (vm_offset_t)addr || end < (vm_offset_t)addr)
19316929939SDon Lewis 		return (EINVAL);
19416929939SDon Lewis 	npages = atop(end - start);
19516929939SDon Lewis 	if (npages > vm_page_max_wired)
19616929939SDon Lewis 		return (ENOMEM);
197ce8660e3SDon Lewis 	PROC_LOCK(curproc);
198bb734798SDon Lewis 	if (ptoa(npages +
199bb734798SDon Lewis 	    pmap_wired_count(vm_map_pmap(&curproc->p_vmspace->vm_map))) >
200bb734798SDon Lewis 	    lim_cur(curproc, RLIMIT_MEMLOCK)) {
201ce8660e3SDon Lewis 		PROC_UNLOCK(curproc);
20216929939SDon Lewis 		return (ENOMEM);
20316929939SDon Lewis 	}
204ce8660e3SDon Lewis 	PROC_UNLOCK(curproc);
20516929939SDon Lewis #if 0
20616929939SDon Lewis 	/*
20716929939SDon Lewis 	 * XXX - not yet
20816929939SDon Lewis 	 *
20916929939SDon Lewis 	 * The limit for transient usage of wired pages should be
21016929939SDon Lewis 	 * larger than for "permanent" wired pages (mlock()).
21116929939SDon Lewis 	 *
21216929939SDon Lewis 	 * Also, the sysctl code, which is the only present user
21316929939SDon Lewis 	 * of vslock(), does a hard loop on EAGAIN.
21416929939SDon Lewis 	 */
21516929939SDon Lewis 	if (npages + cnt.v_wire_count > vm_page_max_wired)
21616929939SDon Lewis 		return (EAGAIN);
21716929939SDon Lewis #endif
218ce8660e3SDon Lewis 	error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end,
219d9b2500eSBrian Feldman 	    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
220ce8660e3SDon Lewis 	/*
221ce8660e3SDon Lewis 	 * Return EFAULT on error to match copy{in,out}() behaviour
222ce8660e3SDon Lewis 	 * rather than returning ENOMEM like mlock() would.
223ce8660e3SDon Lewis 	 */
224ce8660e3SDon Lewis 	return (error == KERN_SUCCESS ? 0 : EFAULT);
22516929939SDon Lewis }
22616929939SDon Lewis 
227ce8660e3SDon Lewis void
228f0ea4612SDon Lewis vsunlock(void *addr, size_t len)
22916929939SDon Lewis {
23016929939SDon Lewis 
231ce8660e3SDon Lewis 	/* Rely on the parameter sanity checks performed by vslock(). */
232ce8660e3SDon Lewis 	(void)vm_map_unwire(&curproc->p_vmspace->vm_map,
233ce8660e3SDon Lewis 	    trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len),
23416929939SDon Lewis 	    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
23516929939SDon Lewis }
23616929939SDon Lewis 
23716929939SDon Lewis /*
238a136efe9SPeter Wemm  * Create the U area for a new process.
239a136efe9SPeter Wemm  * This routine directly affects the fork perf for a process.
240a136efe9SPeter Wemm  */
241a136efe9SPeter Wemm void
242a136efe9SPeter Wemm vm_proc_new(struct proc *p)
243a136efe9SPeter Wemm {
244a136efe9SPeter Wemm 	vm_page_t ma[UAREA_PAGES];
245a136efe9SPeter Wemm 	vm_object_t upobj;
246a136efe9SPeter Wemm 	vm_offset_t up;
247a136efe9SPeter Wemm 	vm_page_t m;
248a136efe9SPeter Wemm 	u_int i;
249a136efe9SPeter Wemm 
250a136efe9SPeter Wemm 	/*
251a136efe9SPeter Wemm 	 * Get a kernel virtual address for the U area for this process.
252a136efe9SPeter Wemm 	 */
253a136efe9SPeter Wemm 	up = kmem_alloc_nofault(kernel_map, UAREA_PAGES * PAGE_SIZE);
254a136efe9SPeter Wemm 	if (up == 0)
255a136efe9SPeter Wemm 		panic("vm_proc_new: upage allocation failed");
256a136efe9SPeter Wemm 	p->p_uarea = (struct user *)up;
257a136efe9SPeter Wemm 
258a136efe9SPeter Wemm 	/*
259ef13663bSAlan Cox 	 * Allocate object and page(s) for the U area.
260a136efe9SPeter Wemm 	 */
261ef13663bSAlan Cox 	upobj = vm_object_allocate(OBJT_DEFAULT, UAREA_PAGES);
262ef13663bSAlan Cox 	p->p_upages_obj = upobj;
263ef13663bSAlan Cox 	VM_OBJECT_LOCK(upobj);
264ef13663bSAlan Cox 	for (i = 0; i < UAREA_PAGES; i++) {
26514f8ceaaSAlan Cox 		m = vm_page_grab(upobj, i,
26614f8ceaaSAlan Cox 		    VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED);
267a136efe9SPeter Wemm 		ma[i] = m;
268a136efe9SPeter Wemm 
269dc907f66SAlan Cox 		vm_page_lock_queues();
270a136efe9SPeter Wemm 		vm_page_wakeup(m);
271a136efe9SPeter Wemm 		m->valid = VM_PAGE_BITS_ALL;
272dc907f66SAlan Cox 		vm_page_unlock_queues();
273a136efe9SPeter Wemm 	}
274ef13663bSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
275a136efe9SPeter Wemm 
276a136efe9SPeter Wemm 	/*
277a136efe9SPeter Wemm 	 * Enter the pages into the kernel address space.
278a136efe9SPeter Wemm 	 */
279a136efe9SPeter Wemm 	pmap_qenter(up, ma, UAREA_PAGES);
280a136efe9SPeter Wemm }
281a136efe9SPeter Wemm 
282a136efe9SPeter Wemm /*
283a136efe9SPeter Wemm  * Dispose the U area for a process that has exited.
284a136efe9SPeter Wemm  * This routine directly impacts the exit perf of a process.
285a136efe9SPeter Wemm  * XXX proc_zone is marked UMA_ZONE_NOFREE, so this should never be called.
286a136efe9SPeter Wemm  */
287a136efe9SPeter Wemm void
288a136efe9SPeter Wemm vm_proc_dispose(struct proc *p)
289a136efe9SPeter Wemm {
290a136efe9SPeter Wemm 	vm_object_t upobj;
291a136efe9SPeter Wemm 	vm_offset_t up;
292a136efe9SPeter Wemm 	vm_page_t m;
293a136efe9SPeter Wemm 
294a136efe9SPeter Wemm 	upobj = p->p_upages_obj;
2956a07e90dSAlan Cox 	VM_OBJECT_LOCK(upobj);
296f59685a4SPeter Wemm 	if (upobj->resident_page_count != UAREA_PAGES)
297f59685a4SPeter Wemm 		panic("vm_proc_dispose: incorrect number of pages in upobj");
2982d09a6adSAlan Cox 	vm_page_lock_queues();
299f59685a4SPeter Wemm 	while ((m = TAILQ_FIRST(&upobj->memq)) != NULL) {
300a136efe9SPeter Wemm 		vm_page_busy(m);
301a136efe9SPeter Wemm 		vm_page_unwire(m, 0);
302a136efe9SPeter Wemm 		vm_page_free(m);
303a136efe9SPeter Wemm 	}
3042d09a6adSAlan Cox 	vm_page_unlock_queues();
3056a07e90dSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
306f59685a4SPeter Wemm 	up = (vm_offset_t)p->p_uarea;
307a136efe9SPeter Wemm 	pmap_qremove(up, UAREA_PAGES);
308a136efe9SPeter Wemm 	kmem_free(kernel_map, up, UAREA_PAGES * PAGE_SIZE);
309a136efe9SPeter Wemm 	vm_object_deallocate(upobj);
310a136efe9SPeter Wemm }
311a136efe9SPeter Wemm 
312a136efe9SPeter Wemm #ifndef NO_SWAPPING
313a136efe9SPeter Wemm /*
314a136efe9SPeter Wemm  * Allow the U area for a process to be prejudicially paged out.
315a136efe9SPeter Wemm  */
31637c84183SPoul-Henning Kamp static void
317a136efe9SPeter Wemm vm_proc_swapout(struct proc *p)
318a136efe9SPeter Wemm {
319a136efe9SPeter Wemm 	vm_object_t upobj;
320a136efe9SPeter Wemm 	vm_offset_t up;
321a136efe9SPeter Wemm 	vm_page_t m;
322a136efe9SPeter Wemm 
323a136efe9SPeter Wemm 	upobj = p->p_upages_obj;
3246a07e90dSAlan Cox 	VM_OBJECT_LOCK(upobj);
325f59685a4SPeter Wemm 	if (upobj->resident_page_count != UAREA_PAGES)
326f59685a4SPeter Wemm 		panic("vm_proc_dispose: incorrect number of pages in upobj");
3272d09a6adSAlan Cox 	vm_page_lock_queues();
328f59685a4SPeter Wemm 	TAILQ_FOREACH(m, &upobj->memq, listq) {
329a136efe9SPeter Wemm 		vm_page_dirty(m);
330a136efe9SPeter Wemm 		vm_page_unwire(m, 0);
331a136efe9SPeter Wemm 	}
3322d09a6adSAlan Cox 	vm_page_unlock_queues();
3336a07e90dSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
334f59685a4SPeter Wemm 	up = (vm_offset_t)p->p_uarea;
335a136efe9SPeter Wemm 	pmap_qremove(up, UAREA_PAGES);
336a136efe9SPeter Wemm }
337a136efe9SPeter Wemm 
338a136efe9SPeter Wemm /*
339a136efe9SPeter Wemm  * Bring the U area for a specified process back in.
340a136efe9SPeter Wemm  */
34137c84183SPoul-Henning Kamp static void
342a136efe9SPeter Wemm vm_proc_swapin(struct proc *p)
343a136efe9SPeter Wemm {
344a136efe9SPeter Wemm 	vm_page_t ma[UAREA_PAGES];
345a136efe9SPeter Wemm 	vm_object_t upobj;
346a136efe9SPeter Wemm 	vm_offset_t up;
347a136efe9SPeter Wemm 	vm_page_t m;
348a136efe9SPeter Wemm 	int rv;
349a136efe9SPeter Wemm 	int i;
350a136efe9SPeter Wemm 
351a136efe9SPeter Wemm 	upobj = p->p_upages_obj;
3528630c117SAlan Cox 	VM_OBJECT_LOCK(upobj);
353a136efe9SPeter Wemm 	for (i = 0; i < UAREA_PAGES; i++) {
354a136efe9SPeter Wemm 		m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
355a136efe9SPeter Wemm 		if (m->valid != VM_PAGE_BITS_ALL) {
356a136efe9SPeter Wemm 			rv = vm_pager_get_pages(upobj, &m, 1, 0);
357a136efe9SPeter Wemm 			if (rv != VM_PAGER_OK)
358a136efe9SPeter Wemm 				panic("vm_proc_swapin: cannot get upage");
359a136efe9SPeter Wemm 		}
360a136efe9SPeter Wemm 		ma[i] = m;
361a7e9138eSPeter Wemm 	}
362a7e9138eSPeter Wemm 	if (upobj->resident_page_count != UAREA_PAGES)
363a7e9138eSPeter Wemm 		panic("vm_proc_swapin: lost pages from upobj");
364e16cfdbeSAlan Cox 	vm_page_lock_queues();
365a7e9138eSPeter Wemm 	TAILQ_FOREACH(m, &upobj->memq, listq) {
366a7e9138eSPeter Wemm 		m->valid = VM_PAGE_BITS_ALL;
367a136efe9SPeter Wemm 		vm_page_wire(m);
368a136efe9SPeter Wemm 		vm_page_wakeup(m);
369a136efe9SPeter Wemm 	}
370e16cfdbeSAlan Cox 	vm_page_unlock_queues();
3716a07e90dSAlan Cox 	VM_OBJECT_UNLOCK(upobj);
372f59685a4SPeter Wemm 	up = (vm_offset_t)p->p_uarea;
373a136efe9SPeter Wemm 	pmap_qenter(up, ma, UAREA_PAGES);
374a136efe9SPeter Wemm }
37592da00bbSMatthew Dillon 
37692da00bbSMatthew Dillon /*
37792da00bbSMatthew Dillon  * Swap in the UAREAs of all processes swapped out to the given device.
37892da00bbSMatthew Dillon  * The pages in the UAREA are marked dirty and their swap metadata is freed.
37992da00bbSMatthew Dillon  */
38092da00bbSMatthew Dillon void
3818f60c087SPoul-Henning Kamp vm_proc_swapin_all(struct swdevt *devidx)
38292da00bbSMatthew Dillon {
38392da00bbSMatthew Dillon 	struct proc *p;
38492da00bbSMatthew Dillon 	vm_object_t object;
38592da00bbSMatthew Dillon 	vm_page_t m;
38692da00bbSMatthew Dillon 
38792da00bbSMatthew Dillon retry:
38892da00bbSMatthew Dillon 	sx_slock(&allproc_lock);
38992da00bbSMatthew Dillon 	FOREACH_PROC_IN_SYSTEM(p) {
39092da00bbSMatthew Dillon 		PROC_LOCK(p);
39192da00bbSMatthew Dillon 		object = p->p_upages_obj;
39217cd3642SAlan Cox 		if (object != NULL) {
39317cd3642SAlan Cox 			VM_OBJECT_LOCK(object);
39417cd3642SAlan Cox 			if (swap_pager_isswapped(object, devidx)) {
39517cd3642SAlan Cox 				VM_OBJECT_UNLOCK(object);
39692da00bbSMatthew Dillon 				sx_sunlock(&allproc_lock);
39792da00bbSMatthew Dillon 				faultin(p);
39892da00bbSMatthew Dillon 				PROC_UNLOCK(p);
3996a07e90dSAlan Cox 				VM_OBJECT_LOCK(object);
40092da00bbSMatthew Dillon 				vm_page_lock_queues();
40192da00bbSMatthew Dillon 				TAILQ_FOREACH(m, &object->memq, listq)
40292da00bbSMatthew Dillon 					vm_page_dirty(m);
40392da00bbSMatthew Dillon 				vm_page_unlock_queues();
40492da00bbSMatthew Dillon 				swap_pager_freespace(object, 0,
40592da00bbSMatthew Dillon 				    object->un_pager.swp.swp_bcount);
4066a07e90dSAlan Cox 				VM_OBJECT_UNLOCK(object);
40792da00bbSMatthew Dillon 				goto retry;
40892da00bbSMatthew Dillon 			}
40917cd3642SAlan Cox 			VM_OBJECT_UNLOCK(object);
41017cd3642SAlan Cox 		}
41192da00bbSMatthew Dillon 		PROC_UNLOCK(p);
41292da00bbSMatthew Dillon 	}
41392da00bbSMatthew Dillon 	sx_sunlock(&allproc_lock);
41492da00bbSMatthew Dillon }
415a136efe9SPeter Wemm #endif
416a136efe9SPeter Wemm 
41749a2507bSAlan Cox #ifndef KSTACK_MAX_PAGES
41849a2507bSAlan Cox #define KSTACK_MAX_PAGES 32
41949a2507bSAlan Cox #endif
42049a2507bSAlan Cox 
42149a2507bSAlan Cox /*
42249a2507bSAlan Cox  * Create the kernel stack (including pcb for i386) for a new thread.
42349a2507bSAlan Cox  * This routine directly affects the fork perf for a process and
42449a2507bSAlan Cox  * create performance for a thread.
42549a2507bSAlan Cox  */
42649a2507bSAlan Cox void
42749a2507bSAlan Cox vm_thread_new(struct thread *td, int pages)
42849a2507bSAlan Cox {
42949a2507bSAlan Cox 	vm_object_t ksobj;
43049a2507bSAlan Cox 	vm_offset_t ks;
43149a2507bSAlan Cox 	vm_page_t m, ma[KSTACK_MAX_PAGES];
43249a2507bSAlan Cox 	int i;
43349a2507bSAlan Cox 
43449a2507bSAlan Cox 	/* Bounds check */
43549a2507bSAlan Cox 	if (pages <= 1)
43649a2507bSAlan Cox 		pages = KSTACK_PAGES;
43749a2507bSAlan Cox 	else if (pages > KSTACK_MAX_PAGES)
43849a2507bSAlan Cox 		pages = KSTACK_MAX_PAGES;
43949a2507bSAlan Cox 	/*
44049a2507bSAlan Cox 	 * Allocate an object for the kstack.
44149a2507bSAlan Cox 	 */
44249a2507bSAlan Cox 	ksobj = vm_object_allocate(OBJT_DEFAULT, pages);
44349a2507bSAlan Cox 	td->td_kstack_obj = ksobj;
44449a2507bSAlan Cox 	/*
44549a2507bSAlan Cox 	 * Get a kernel virtual address for this thread's kstack.
44649a2507bSAlan Cox 	 */
44749a2507bSAlan Cox 	ks = kmem_alloc_nofault(kernel_map,
44849a2507bSAlan Cox 	   (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
44949a2507bSAlan Cox 	if (ks == 0)
45049a2507bSAlan Cox 		panic("vm_thread_new: kstack allocation failed");
45149a2507bSAlan Cox 	if (KSTACK_GUARD_PAGES != 0) {
45249a2507bSAlan Cox 		pmap_qremove(ks, KSTACK_GUARD_PAGES);
45349a2507bSAlan Cox 		ks += KSTACK_GUARD_PAGES * PAGE_SIZE;
45449a2507bSAlan Cox 	}
45549a2507bSAlan Cox 	td->td_kstack = ks;
45649a2507bSAlan Cox 	/*
45749a2507bSAlan Cox 	 * Knowing the number of pages allocated is useful when you
45849a2507bSAlan Cox 	 * want to deallocate them.
45949a2507bSAlan Cox 	 */
46049a2507bSAlan Cox 	td->td_kstack_pages = pages;
46149a2507bSAlan Cox 	/*
46249a2507bSAlan Cox 	 * For the length of the stack, link in a real page of ram for each
46349a2507bSAlan Cox 	 * page of stack.
46449a2507bSAlan Cox 	 */
46549a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
46649a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
46749a2507bSAlan Cox 		/*
46849a2507bSAlan Cox 		 * Get a kernel stack page.
46949a2507bSAlan Cox 		 */
47049a2507bSAlan Cox 		m = vm_page_grab(ksobj, i,
47149a2507bSAlan Cox 		    VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED);
47249a2507bSAlan Cox 		ma[i] = m;
47349a2507bSAlan Cox 		vm_page_lock_queues();
47449a2507bSAlan Cox 		vm_page_wakeup(m);
47549a2507bSAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
47649a2507bSAlan Cox 		vm_page_unlock_queues();
47749a2507bSAlan Cox 	}
47849a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
47949a2507bSAlan Cox 	pmap_qenter(ks, ma, pages);
48049a2507bSAlan Cox }
48149a2507bSAlan Cox 
48249a2507bSAlan Cox /*
48349a2507bSAlan Cox  * Dispose of a thread's kernel stack.
48449a2507bSAlan Cox  */
48549a2507bSAlan Cox void
48649a2507bSAlan Cox vm_thread_dispose(struct thread *td)
48749a2507bSAlan Cox {
48849a2507bSAlan Cox 	vm_object_t ksobj;
48949a2507bSAlan Cox 	vm_offset_t ks;
49049a2507bSAlan Cox 	vm_page_t m;
49149a2507bSAlan Cox 	int i, pages;
49249a2507bSAlan Cox 
49349a2507bSAlan Cox 	pages = td->td_kstack_pages;
49449a2507bSAlan Cox 	ksobj = td->td_kstack_obj;
49549a2507bSAlan Cox 	ks = td->td_kstack;
49649a2507bSAlan Cox 	pmap_qremove(ks, pages);
49749a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
49849a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
49949a2507bSAlan Cox 		m = vm_page_lookup(ksobj, i);
50049a2507bSAlan Cox 		if (m == NULL)
50149a2507bSAlan Cox 			panic("vm_thread_dispose: kstack already missing?");
50249a2507bSAlan Cox 		vm_page_lock_queues();
50349a2507bSAlan Cox 		vm_page_busy(m);
50449a2507bSAlan Cox 		vm_page_unwire(m, 0);
50549a2507bSAlan Cox 		vm_page_free(m);
50649a2507bSAlan Cox 		vm_page_unlock_queues();
50749a2507bSAlan Cox 	}
50849a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
50949a2507bSAlan Cox 	vm_object_deallocate(ksobj);
51049a2507bSAlan Cox 	kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE),
51149a2507bSAlan Cox 	    (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
51249a2507bSAlan Cox }
51349a2507bSAlan Cox 
51449a2507bSAlan Cox /*
51549a2507bSAlan Cox  * Allow a thread's kernel stack to be paged out.
51649a2507bSAlan Cox  */
51749a2507bSAlan Cox void
51849a2507bSAlan Cox vm_thread_swapout(struct thread *td)
51949a2507bSAlan Cox {
52049a2507bSAlan Cox 	vm_object_t ksobj;
52149a2507bSAlan Cox 	vm_page_t m;
52249a2507bSAlan Cox 	int i, pages;
52349a2507bSAlan Cox 
524710338e9SMarcel Moolenaar 	cpu_thread_swapout(td);
52549a2507bSAlan Cox 	pages = td->td_kstack_pages;
52649a2507bSAlan Cox 	ksobj = td->td_kstack_obj;
52749a2507bSAlan Cox 	pmap_qremove(td->td_kstack, pages);
52849a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
52949a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
53049a2507bSAlan Cox 		m = vm_page_lookup(ksobj, i);
53149a2507bSAlan Cox 		if (m == NULL)
53249a2507bSAlan Cox 			panic("vm_thread_swapout: kstack already missing?");
53349a2507bSAlan Cox 		vm_page_lock_queues();
53449a2507bSAlan Cox 		vm_page_dirty(m);
53549a2507bSAlan Cox 		vm_page_unwire(m, 0);
53649a2507bSAlan Cox 		vm_page_unlock_queues();
53749a2507bSAlan Cox 	}
53849a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
53949a2507bSAlan Cox }
54049a2507bSAlan Cox 
54149a2507bSAlan Cox /*
54249a2507bSAlan Cox  * Bring the kernel stack for a specified thread back in.
54349a2507bSAlan Cox  */
54449a2507bSAlan Cox void
54549a2507bSAlan Cox vm_thread_swapin(struct thread *td)
54649a2507bSAlan Cox {
54749a2507bSAlan Cox 	vm_object_t ksobj;
54849a2507bSAlan Cox 	vm_page_t m, ma[KSTACK_MAX_PAGES];
54949a2507bSAlan Cox 	int i, pages, rv;
55049a2507bSAlan Cox 
55149a2507bSAlan Cox 	pages = td->td_kstack_pages;
55249a2507bSAlan Cox 	ksobj = td->td_kstack_obj;
55349a2507bSAlan Cox 	VM_OBJECT_LOCK(ksobj);
55449a2507bSAlan Cox 	for (i = 0; i < pages; i++) {
55549a2507bSAlan Cox 		m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
55649a2507bSAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
55749a2507bSAlan Cox 			rv = vm_pager_get_pages(ksobj, &m, 1, 0);
55849a2507bSAlan Cox 			if (rv != VM_PAGER_OK)
55949a2507bSAlan Cox 				panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid);
56049a2507bSAlan Cox 			m = vm_page_lookup(ksobj, i);
56149a2507bSAlan Cox 			m->valid = VM_PAGE_BITS_ALL;
56249a2507bSAlan Cox 		}
56349a2507bSAlan Cox 		ma[i] = m;
56449a2507bSAlan Cox 		vm_page_lock_queues();
56549a2507bSAlan Cox 		vm_page_wire(m);
56649a2507bSAlan Cox 		vm_page_wakeup(m);
56749a2507bSAlan Cox 		vm_page_unlock_queues();
56849a2507bSAlan Cox 	}
56949a2507bSAlan Cox 	VM_OBJECT_UNLOCK(ksobj);
57049a2507bSAlan Cox 	pmap_qenter(td->td_kstack, ma, pages);
571710338e9SMarcel Moolenaar 	cpu_thread_swapin(td);
57249a2507bSAlan Cox }
57349a2507bSAlan Cox 
574a136efe9SPeter Wemm /*
57589f4fca2SAlan Cox  * Set up a variable-sized alternate kstack.
57689f4fca2SAlan Cox  */
57789f4fca2SAlan Cox void
57889f4fca2SAlan Cox vm_thread_new_altkstack(struct thread *td, int pages)
57989f4fca2SAlan Cox {
58089f4fca2SAlan Cox 
58189f4fca2SAlan Cox 	td->td_altkstack = td->td_kstack;
58289f4fca2SAlan Cox 	td->td_altkstack_obj = td->td_kstack_obj;
58389f4fca2SAlan Cox 	td->td_altkstack_pages = td->td_kstack_pages;
58489f4fca2SAlan Cox 
58549a2507bSAlan Cox 	vm_thread_new(td, pages);
58689f4fca2SAlan Cox }
58789f4fca2SAlan Cox 
58889f4fca2SAlan Cox /*
58989f4fca2SAlan Cox  * Restore the original kstack.
59089f4fca2SAlan Cox  */
59189f4fca2SAlan Cox void
59289f4fca2SAlan Cox vm_thread_dispose_altkstack(struct thread *td)
59389f4fca2SAlan Cox {
59489f4fca2SAlan Cox 
59549a2507bSAlan Cox 	vm_thread_dispose(td);
59689f4fca2SAlan Cox 
59789f4fca2SAlan Cox 	td->td_kstack = td->td_altkstack;
59889f4fca2SAlan Cox 	td->td_kstack_obj = td->td_altkstack_obj;
59989f4fca2SAlan Cox 	td->td_kstack_pages = td->td_altkstack_pages;
60089f4fca2SAlan Cox 	td->td_altkstack = 0;
60189f4fca2SAlan Cox 	td->td_altkstack_obj = NULL;
60289f4fca2SAlan Cox 	td->td_altkstack_pages = 0;
60389f4fca2SAlan Cox }
60489f4fca2SAlan Cox 
60589f4fca2SAlan Cox /*
606df8bae1dSRodney W. Grimes  * Implement fork's actions on an address space.
607df8bae1dSRodney W. Grimes  * Here we arrange for the address space to be copied or referenced,
608df8bae1dSRodney W. Grimes  * allocate a user struct (pcb and kernel stack), then call the
609df8bae1dSRodney W. Grimes  * machine-dependent layer to fill those in and make the new process
610a2a1c95cSPeter Wemm  * ready to run.  The new process is set up so that it returns directly
611a2a1c95cSPeter Wemm  * to user mode to avoid stack copying and relocation problems.
612df8bae1dSRodney W. Grimes  */
613a2a1c95cSPeter Wemm void
614079b7badSJulian Elischer vm_forkproc(td, p2, td2, flags)
615b40ce416SJulian Elischer 	struct thread *td;
616b40ce416SJulian Elischer 	struct proc *p2;
617079b7badSJulian Elischer 	struct thread *td2;
618a2a1c95cSPeter Wemm 	int flags;
619df8bae1dSRodney W. Grimes {
620b40ce416SJulian Elischer 	struct proc *p1 = td->td_proc;
62154d92145SMatthew Dillon 	struct user *up;
622df8bae1dSRodney W. Grimes 
6230cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6240cddd8f0SMatthew Dillon 
62591c28bfdSLuoqi Chen 	if ((flags & RFPROC) == 0) {
62691c28bfdSLuoqi Chen 		/*
62791c28bfdSLuoqi Chen 		 * Divorce the memory, if it is shared, essentially
62891c28bfdSLuoqi Chen 		 * this changes shared memory amongst threads, into
62991c28bfdSLuoqi Chen 		 * COW locally.
63091c28bfdSLuoqi Chen 		 */
63191c28bfdSLuoqi Chen 		if ((flags & RFMEM) == 0) {
63291c28bfdSLuoqi Chen 			if (p1->p_vmspace->vm_refcnt > 1) {
63391c28bfdSLuoqi Chen 				vmspace_unshare(p1);
63491c28bfdSLuoqi Chen 			}
63591c28bfdSLuoqi Chen 		}
636079b7badSJulian Elischer 		cpu_fork(td, p2, td2, flags);
63791c28bfdSLuoqi Chen 		return;
63891c28bfdSLuoqi Chen 	}
63991c28bfdSLuoqi Chen 
6405856e12eSJohn Dyson 	if (flags & RFMEM) {
6415856e12eSJohn Dyson 		p2->p_vmspace = p1->p_vmspace;
6425856e12eSJohn Dyson 		p1->p_vmspace->vm_refcnt++;
6435856e12eSJohn Dyson 	}
6445856e12eSJohn Dyson 
64590ecac61SMatthew Dillon 	while (vm_page_count_severe()) {
64626f9a767SRodney W. Grimes 		VM_WAIT;
6470d94caffSDavid Greenman 	}
64826f9a767SRodney W. Grimes 
6495856e12eSJohn Dyson 	if ((flags & RFMEM) == 0) {
650df8bae1dSRodney W. Grimes 		p2->p_vmspace = vmspace_fork(p1->p_vmspace);
651df8bae1dSRodney W. Grimes 		if (p1->p_vmspace->vm_shm)
652dabee6feSPeter Wemm 			shmfork(p1, p2);
653a2a1c95cSPeter Wemm 	}
654df8bae1dSRodney W. Grimes 
655b40ce416SJulian Elischer 	/* XXXKSE this is unsatisfactory but should be adequate */
656b40ce416SJulian Elischer 	up = p2->p_uarea;
65790af4afaSJohn Baldwin 	MPASS(p2->p_sigacts != NULL);
658df8bae1dSRodney W. Grimes 
65939fb8e6bSJulian Elischer 	/*
66039fb8e6bSJulian Elischer 	 * p_stats currently points at fields in the user struct
66139fb8e6bSJulian Elischer 	 * but not at &u, instead at p_addr. Copy parts of
66239fb8e6bSJulian Elischer 	 * p_stats; zero the rest of p_stats (statistics).
66339fb8e6bSJulian Elischer 	 */
66439fb8e6bSJulian Elischer 	p2->p_stats = &up->u_stats;
665df8bae1dSRodney W. Grimes 	bzero(&up->u_stats.pstat_startzero,
666df8bae1dSRodney W. Grimes 	    (unsigned) ((caddr_t) &up->u_stats.pstat_endzero -
667df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startzero));
668df8bae1dSRodney W. Grimes 	bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy,
669df8bae1dSRodney W. Grimes 	    ((caddr_t) &up->u_stats.pstat_endcopy -
670df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startcopy));
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes 	/*
673a2a1c95cSPeter Wemm 	 * cpu_fork will copy and update the pcb, set up the kernel stack,
674a2a1c95cSPeter Wemm 	 * and make the child ready to run.
675df8bae1dSRodney W. Grimes 	 */
676079b7badSJulian Elischer 	cpu_fork(td, p2, td2, flags);
677df8bae1dSRodney W. Grimes }
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes /*
680eb30c1c0SPeter Wemm  * Called after process has been wait(2)'ed apon and is being reaped.
681eb30c1c0SPeter Wemm  * The idea is to reclaim resources that we could not reclaim while
682eb30c1c0SPeter Wemm  * the process was still executing.
683eb30c1c0SPeter Wemm  */
684eb30c1c0SPeter Wemm void
685eb30c1c0SPeter Wemm vm_waitproc(p)
686eb30c1c0SPeter Wemm 	struct proc *p;
687eb30c1c0SPeter Wemm {
688eb30c1c0SPeter Wemm 
689eb30c1c0SPeter Wemm 	GIANT_REQUIRED;
690582ec34cSAlfred Perlstein 	vmspace_exitfree(p);		/* and clean-out the vmspace */
691eb30c1c0SPeter Wemm }
692eb30c1c0SPeter Wemm 
693eb30c1c0SPeter Wemm /*
694df8bae1dSRodney W. Grimes  * Set default limits for VM system.
695df8bae1dSRodney W. Grimes  * Called for proc 0, and then inherited by all others.
6962b14f991SJulian Elischer  *
6972b14f991SJulian Elischer  * XXX should probably act directly on proc0.
698df8bae1dSRodney W. Grimes  */
6992b14f991SJulian Elischer static void
7002b14f991SJulian Elischer vm_init_limits(udata)
7014590fd3aSDavid Greenman 	void *udata;
702df8bae1dSRodney W. Grimes {
70354d92145SMatthew Dillon 	struct proc *p = udata;
70491d5354aSJohn Baldwin 	struct plimit *limp;
705bbc0ec52SDavid Greenman 	int rss_limit;
706df8bae1dSRodney W. Grimes 
707df8bae1dSRodney W. Grimes 	/*
7080d94caffSDavid Greenman 	 * Set up the initial limits on process VM. Set the maximum resident
7090d94caffSDavid Greenman 	 * set size to be half of (reasonably) available memory.  Since this
7100d94caffSDavid Greenman 	 * is a soft limit, it comes into effect only when the system is out
7110d94caffSDavid Greenman 	 * of memory - half of main memory helps to favor smaller processes,
712bbc0ec52SDavid Greenman 	 * and reduces thrashing of the object cache.
713df8bae1dSRodney W. Grimes 	 */
71491d5354aSJohn Baldwin 	limp = p->p_limit;
71591d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz;
71691d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz;
71791d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
71891d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
719dd0bd066SDavid Greenman 	/* limit the limit to no less than 2MB */
720f2daac0cSDavid Greenman 	rss_limit = max(cnt.v_free_count, 512);
72191d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
72291d5354aSJohn Baldwin 	limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
723df8bae1dSRodney W. Grimes }
724df8bae1dSRodney W. Grimes 
72526f9a767SRodney W. Grimes void
72626f9a767SRodney W. Grimes faultin(p)
72726f9a767SRodney W. Grimes 	struct proc *p;
72826f9a767SRodney W. Grimes {
72911edc1e0SJohn Baldwin #ifdef NO_SWAPPING
73011edc1e0SJohn Baldwin 
73111edc1e0SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
73211edc1e0SJohn Baldwin 	if ((p->p_sflag & PS_INMEM) == 0)
73311edc1e0SJohn Baldwin 		panic("faultin: proc swapped out with NO_SWAPPING!");
73411edc1e0SJohn Baldwin #else /* !NO_SWAPPING */
735664f718bSJohn Baldwin 	struct thread *td;
73626f9a767SRodney W. Grimes 
737a136efe9SPeter Wemm 	GIANT_REQUIRED;
738c96d52a9SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
7391d7b9ed2SJulian Elischer 	/*
7401d7b9ed2SJulian Elischer 	 * If another process is swapping in this process,
7411d7b9ed2SJulian Elischer 	 * just wait until it finishes.
7421d7b9ed2SJulian Elischer 	 */
743664f718bSJohn Baldwin 	if (p->p_sflag & PS_SWAPPINGIN)
7441d7b9ed2SJulian Elischer 		msleep(&p->p_sflag, &p->p_mtx, PVM, "faultin", 0);
745664f718bSJohn Baldwin 	else if ((p->p_sflag & PS_INMEM) == 0) {
746664f718bSJohn Baldwin 		/*
747664f718bSJohn Baldwin 		 * Don't let another thread swap process p out while we are
748664f718bSJohn Baldwin 		 * busy swapping it in.
749664f718bSJohn Baldwin 		 */
750664f718bSJohn Baldwin 		++p->p_lock;
7511d7b9ed2SJulian Elischer 		mtx_lock_spin(&sched_lock);
7521d7b9ed2SJulian Elischer 		p->p_sflag |= PS_SWAPPINGIN;
7539ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
75445ece682SJohn Baldwin 		PROC_UNLOCK(p);
75526f9a767SRodney W. Grimes 
756a136efe9SPeter Wemm 		vm_proc_swapin(p);
757664f718bSJohn Baldwin 		FOREACH_THREAD_IN_PROC(p, td)
75849a2507bSAlan Cox 			vm_thread_swapin(td);
75926f9a767SRodney W. Grimes 
76045ece682SJohn Baldwin 		PROC_LOCK(p);
7619ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
7629eb881f8SSeigo Tanimura 		p->p_sflag &= ~PS_SWAPPINGIN;
7639eb881f8SSeigo Tanimura 		p->p_sflag |= PS_INMEM;
764664f718bSJohn Baldwin 		FOREACH_THREAD_IN_PROC(p, td) {
765664f718bSJohn Baldwin 			TD_CLR_SWAPPED(td);
76671fad9fdSJulian Elischer 			if (TD_CAN_RUN(td))
76771fad9fdSJulian Elischer 				setrunnable(td);
768664f718bSJohn Baldwin 		}
769664f718bSJohn Baldwin 		mtx_unlock_spin(&sched_lock);
77026f9a767SRodney W. Grimes 
7711d7b9ed2SJulian Elischer 		wakeup(&p->p_sflag);
77226f9a767SRodney W. Grimes 
773664f718bSJohn Baldwin 		/* Allow other threads to swap p out now. */
77426f9a767SRodney W. Grimes 		--p->p_lock;
77526f9a767SRodney W. Grimes 	}
77611edc1e0SJohn Baldwin #endif /* NO_SWAPPING */
77726f9a767SRodney W. Grimes }
77826f9a767SRodney W. Grimes 
779df8bae1dSRodney W. Grimes /*
78026f9a767SRodney W. Grimes  * This swapin algorithm attempts to swap-in processes only if there
78126f9a767SRodney W. Grimes  * is enough space for them.  Of course, if a process waits for a long
78226f9a767SRodney W. Grimes  * time, it will be swapped in anyway.
7830384fff8SJason Evans  *
784e602ba25SJulian Elischer  *  XXXKSE - process with the thread with highest priority counts..
785b40ce416SJulian Elischer  *
7860384fff8SJason Evans  * Giant is still held at this point, to be released in tsleep.
787df8bae1dSRodney W. Grimes  */
7882b14f991SJulian Elischer /* ARGSUSED*/
7892b14f991SJulian Elischer static void
790d841aaa7SBruce Evans scheduler(dummy)
791d841aaa7SBruce Evans 	void *dummy;
792df8bae1dSRodney W. Grimes {
79354d92145SMatthew Dillon 	struct proc *p;
794e602ba25SJulian Elischer 	struct thread *td;
79554d92145SMatthew Dillon 	int pri;
796df8bae1dSRodney W. Grimes 	struct proc *pp;
797df8bae1dSRodney W. Grimes 	int ppri;
798df8bae1dSRodney W. Grimes 
799c96d52a9SJohn Baldwin 	mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED);
8000cddd8f0SMatthew Dillon 	/* GIANT_REQUIRED */
8010384fff8SJason Evans 
802df8bae1dSRodney W. Grimes loop:
80390ecac61SMatthew Dillon 	if (vm_page_count_min()) {
8040d94caffSDavid Greenman 		VM_WAIT;
80590ecac61SMatthew Dillon 		goto loop;
8060d94caffSDavid Greenman 	}
80726f9a767SRodney W. Grimes 
808df8bae1dSRodney W. Grimes 	pp = NULL;
809df8bae1dSRodney W. Grimes 	ppri = INT_MIN;
8101005a129SJohn Baldwin 	sx_slock(&allproc_lock);
811b40ce416SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
812b40ce416SJulian Elischer 		struct ksegrp *kg;
813664f718bSJohn Baldwin 		if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) {
814e602ba25SJulian Elischer 			continue;
815e602ba25SJulian Elischer 		}
8169ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
817e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
8181d7b9ed2SJulian Elischer 			/*
81971fad9fdSJulian Elischer 			 * An otherwise runnable thread of a process
82071fad9fdSJulian Elischer 			 * swapped out has only the TDI_SWAPPED bit set.
82171fad9fdSJulian Elischer 			 *
8221d7b9ed2SJulian Elischer 			 */
82371fad9fdSJulian Elischer 			if (td->td_inhibitors == TDI_SWAPPED) {
824e602ba25SJulian Elischer 				kg = td->td_ksegrp;
825b40ce416SJulian Elischer 				pri = p->p_swtime + kg->kg_slptime;
8265074aecdSJohn Baldwin 				if ((p->p_sflag & PS_SWAPINREQ) == 0) {
827b40ce416SJulian Elischer 					pri -= kg->kg_nice * 8;
828a669a6e9SJohn Dyson 				}
82995461b45SJohn Dyson 
83026f9a767SRodney W. Grimes 				/*
831b40ce416SJulian Elischer 				 * if this ksegrp is higher priority
832b40ce416SJulian Elischer 				 * and there is enough space, then select
833b40ce416SJulian Elischer 				 * this process instead of the previous
834b40ce416SJulian Elischer 				 * selection.
83526f9a767SRodney W. Grimes 				 */
8360d94caffSDavid Greenman 				if (pri > ppri) {
837df8bae1dSRodney W. Grimes 					pp = p;
838df8bae1dSRodney W. Grimes 					ppri = pri;
839df8bae1dSRodney W. Grimes 				}
840df8bae1dSRodney W. Grimes 			}
841b40ce416SJulian Elischer 		}
8429ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
843df8bae1dSRodney W. Grimes 	}
8441005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
84526f9a767SRodney W. Grimes 
846df8bae1dSRodney W. Grimes 	/*
847a669a6e9SJohn Dyson 	 * Nothing to do, back to sleep.
848df8bae1dSRodney W. Grimes 	 */
849df8bae1dSRodney W. Grimes 	if ((p = pp) == NULL) {
850ea754954SJohn Baldwin 		tsleep(&proc0, PVM, "sched", maxslp * hz / 2);
851df8bae1dSRodney W. Grimes 		goto loop;
852df8bae1dSRodney W. Grimes 	}
8531d7b9ed2SJulian Elischer 	PROC_LOCK(p);
8541d7b9ed2SJulian Elischer 
8551d7b9ed2SJulian Elischer 	/*
8561d7b9ed2SJulian Elischer 	 * Another process may be bringing or may have already
8571d7b9ed2SJulian Elischer 	 * brought this process in while we traverse all threads.
8581d7b9ed2SJulian Elischer 	 * Or, this process may even be being swapped out again.
8591d7b9ed2SJulian Elischer 	 */
860664f718bSJohn Baldwin 	if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) {
8611d7b9ed2SJulian Elischer 		PROC_UNLOCK(p);
8621d7b9ed2SJulian Elischer 		goto loop;
8631d7b9ed2SJulian Elischer 	}
8641d7b9ed2SJulian Elischer 
865664f718bSJohn Baldwin 	mtx_lock_spin(&sched_lock);
8661d7b9ed2SJulian Elischer 	p->p_sflag &= ~PS_SWAPINREQ;
867664f718bSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
868a669a6e9SJohn Dyson 
869df8bae1dSRodney W. Grimes 	/*
87026f9a767SRodney W. Grimes 	 * We would like to bring someone in. (only if there is space).
871e602ba25SJulian Elischer 	 * [What checks the space? ]
872df8bae1dSRodney W. Grimes 	 */
87326f9a767SRodney W. Grimes 	faultin(p);
87445ece682SJohn Baldwin 	PROC_UNLOCK(p);
875664f718bSJohn Baldwin 	mtx_lock_spin(&sched_lock);
876df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
8779ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
878df8bae1dSRodney W. Grimes 	goto loop;
879df8bae1dSRodney W. Grimes }
880df8bae1dSRodney W. Grimes 
8815afce282SDavid Greenman #ifndef NO_SWAPPING
8825afce282SDavid Greenman 
883ceb0cf87SJohn Dyson /*
884ceb0cf87SJohn Dyson  * Swap_idle_threshold1 is the guaranteed swapped in time for a process
885ceb0cf87SJohn Dyson  */
886303b270bSEivind Eklund static int swap_idle_threshold1 = 2;
8872a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW,
8889faaf3b3STom Rhodes     &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process");
889ceb0cf87SJohn Dyson 
890ceb0cf87SJohn Dyson /*
891ceb0cf87SJohn Dyson  * Swap_idle_threshold2 is the time that a process can be idle before
892ceb0cf87SJohn Dyson  * it will be swapped out, if idle swapping is enabled.
893ceb0cf87SJohn Dyson  */
894303b270bSEivind Eklund static int swap_idle_threshold2 = 10;
8952a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW,
8969faaf3b3STom Rhodes     &swap_idle_threshold2, 0, "Time before a process will be swapped out");
897ceb0cf87SJohn Dyson 
898df8bae1dSRodney W. Grimes /*
899df8bae1dSRodney W. Grimes  * Swapout is driven by the pageout daemon.  Very simple, we find eligible
900df8bae1dSRodney W. Grimes  * procs and unwire their u-areas.  We try to always "swap" at least one
901df8bae1dSRodney W. Grimes  * process in case we need the room for a swapin.
902df8bae1dSRodney W. Grimes  * If any procs have been sleeping/stopped for at least maxslp seconds,
903df8bae1dSRodney W. Grimes  * they are swapped.  Else, we swap the longest-sleeping or stopped process,
904df8bae1dSRodney W. Grimes  * if any, otherwise the longest-resident process.
905df8bae1dSRodney W. Grimes  */
906df8bae1dSRodney W. Grimes void
9073a2dc656SJohn Dyson swapout_procs(action)
9083a2dc656SJohn Dyson int action;
909df8bae1dSRodney W. Grimes {
91054d92145SMatthew Dillon 	struct proc *p;
911e602ba25SJulian Elischer 	struct thread *td;
912b40ce416SJulian Elischer 	struct ksegrp *kg;
913df8bae1dSRodney W. Grimes 	int didswap = 0;
914df8bae1dSRodney W. Grimes 
9150cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
9160cddd8f0SMatthew Dillon 
9170d94caffSDavid Greenman retry:
9183a2189d4SJohn Baldwin 	sx_slock(&allproc_lock);
919e602ba25SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
920b18bfc3dSJohn Dyson 		struct vmspace *vm;
921b40ce416SJulian Elischer 		int minslptime = 100000;
922b18bfc3dSJohn Dyson 
9239eb881f8SSeigo Tanimura 		/*
924b1f99ebeSSeigo Tanimura 		 * Watch out for a process in
925b1f99ebeSSeigo Tanimura 		 * creation.  It may have no
9261c865ac7SJohn Baldwin 		 * address space or lock yet.
9271c865ac7SJohn Baldwin 		 */
9281c865ac7SJohn Baldwin 		mtx_lock_spin(&sched_lock);
9291c865ac7SJohn Baldwin 		if (p->p_state == PRS_NEW) {
9301c865ac7SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
9311c865ac7SJohn Baldwin 			continue;
9321c865ac7SJohn Baldwin 		}
9331c865ac7SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
9341c865ac7SJohn Baldwin 
9351c865ac7SJohn Baldwin 		/*
936b1f99ebeSSeigo Tanimura 		 * An aio daemon switches its
937b1f99ebeSSeigo Tanimura 		 * address space while running.
938b1f99ebeSSeigo Tanimura 		 * Perform a quick check whether
939b1f99ebeSSeigo Tanimura 		 * a process has P_SYSTEM.
9409eb881f8SSeigo Tanimura 		 */
9418f887403SJohn Baldwin 		if ((p->p_flag & P_SYSTEM) != 0)
942b1f99ebeSSeigo Tanimura 			continue;
9431c865ac7SJohn Baldwin 
9441c865ac7SJohn Baldwin 		/*
9451c865ac7SJohn Baldwin 		 * Do not swapout a process that
9461c865ac7SJohn Baldwin 		 * is waiting for VM data
9471c865ac7SJohn Baldwin 		 * structures as there is a possible
9481c865ac7SJohn Baldwin 		 * deadlock.  Test this first as
9491c865ac7SJohn Baldwin 		 * this may block.
9501c865ac7SJohn Baldwin 		 *
9511c865ac7SJohn Baldwin 		 * Lock the map until swapout
9521c865ac7SJohn Baldwin 		 * finishes, or a thread of this
9531c865ac7SJohn Baldwin 		 * process may attempt to alter
9541c865ac7SJohn Baldwin 		 * the map.
9551c865ac7SJohn Baldwin 		 */
9568f887403SJohn Baldwin 		PROC_LOCK(p);
9579eb881f8SSeigo Tanimura 		vm = p->p_vmspace;
958b1f99ebeSSeigo Tanimura 		KASSERT(vm != NULL,
959b1f99ebeSSeigo Tanimura 			("swapout_procs: a process has no address space"));
9609eb881f8SSeigo Tanimura 		++vm->vm_refcnt;
961b1f99ebeSSeigo Tanimura 		PROC_UNLOCK(p);
9629eb881f8SSeigo Tanimura 		if (!vm_map_trylock(&vm->vm_map))
9639eb881f8SSeigo Tanimura 			goto nextproc1;
9649eb881f8SSeigo Tanimura 
9655074aecdSJohn Baldwin 		PROC_LOCK(p);
96669b40456SJohn Baldwin 		if (p->p_lock != 0 ||
9671279572aSDavid Xu 		    (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT)
9681279572aSDavid Xu 		    ) != 0) {
9699eb881f8SSeigo Tanimura 			goto nextproc2;
9705074aecdSJohn Baldwin 		}
97123955314SAlfred Perlstein 		/*
97223955314SAlfred Perlstein 		 * only aiod changes vmspace, however it will be
97323955314SAlfred Perlstein 		 * skipped because of the if statement above checking
97423955314SAlfred Perlstein 		 * for P_SYSTEM
97523955314SAlfred Perlstein 		 */
976664f718bSJohn Baldwin 		if ((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) != PS_INMEM)
977664f718bSJohn Baldwin 			goto nextproc2;
97869b40456SJohn Baldwin 
979e602ba25SJulian Elischer 		switch (p->p_state) {
9800d94caffSDavid Greenman 		default:
981e602ba25SJulian Elischer 			/* Don't swap out processes in any sort
982e602ba25SJulian Elischer 			 * of 'special' state. */
9838f887403SJohn Baldwin 			break;
984df8bae1dSRodney W. Grimes 
985e602ba25SJulian Elischer 		case PRS_NORMAL:
9868f887403SJohn Baldwin 			mtx_lock_spin(&sched_lock);
98726f9a767SRodney W. Grimes 			/*
988bfbfac11SDavid Greenman 			 * do not swapout a realtime process
989b40ce416SJulian Elischer 			 * Check all the thread groups..
990bfbfac11SDavid Greenman 			 */
991b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(p, kg) {
9929eb881f8SSeigo Tanimura 				if (PRI_IS_REALTIME(kg->kg_pri_class))
993b40ce416SJulian Elischer 					goto nextproc;
994bfbfac11SDavid Greenman 
995bfbfac11SDavid Greenman 				/*
9969eb881f8SSeigo Tanimura 				 * Guarantee swap_idle_threshold1
997ceb0cf87SJohn Dyson 				 * time in memory.
9980d94caffSDavid Greenman 				 */
9999eb881f8SSeigo Tanimura 				if (kg->kg_slptime < swap_idle_threshold1)
1000b40ce416SJulian Elischer 					goto nextproc;
10019eb881f8SSeigo Tanimura 
10021d7b9ed2SJulian Elischer 				/*
10039eb881f8SSeigo Tanimura 				 * Do not swapout a process if it is
10049eb881f8SSeigo Tanimura 				 * waiting on a critical event of some
10059eb881f8SSeigo Tanimura 				 * kind or there is a thread whose
10069eb881f8SSeigo Tanimura 				 * pageable memory may be accessed.
10071d7b9ed2SJulian Elischer 				 *
10081d7b9ed2SJulian Elischer 				 * This could be refined to support
10091d7b9ed2SJulian Elischer 				 * swapping out a thread.
10101d7b9ed2SJulian Elischer 				 */
10119eb881f8SSeigo Tanimura 				FOREACH_THREAD_IN_GROUP(kg, td) {
10121d7b9ed2SJulian Elischer 					if ((td->td_priority) < PSOCK ||
10139eb881f8SSeigo Tanimura 					    !thread_safetoswapout(td))
1014e602ba25SJulian Elischer 						goto nextproc;
1015e602ba25SJulian Elischer 				}
1016ceb0cf87SJohn Dyson 				/*
1017b40ce416SJulian Elischer 				 * If the system is under memory stress,
1018b40ce416SJulian Elischer 				 * or if we are swapping
1019b40ce416SJulian Elischer 				 * idle processes >= swap_idle_threshold2,
1020b40ce416SJulian Elischer 				 * then swap the process out.
1021ceb0cf87SJohn Dyson 				 */
1022ceb0cf87SJohn Dyson 				if (((action & VM_SWAP_NORMAL) == 0) &&
1023ceb0cf87SJohn Dyson 				    (((action & VM_SWAP_IDLE) == 0) ||
10249eb881f8SSeigo Tanimura 				    (kg->kg_slptime < swap_idle_threshold2)))
1025b40ce416SJulian Elischer 					goto nextproc;
10269eb881f8SSeigo Tanimura 
1027b40ce416SJulian Elischer 				if (minslptime > kg->kg_slptime)
1028b40ce416SJulian Elischer 					minslptime = kg->kg_slptime;
1029b40ce416SJulian Elischer 			}
10300d94caffSDavid Greenman 
103111b224dcSDavid Greenman 			/*
10320d94caffSDavid Greenman 			 * If the process has been asleep for awhile and had
10330d94caffSDavid Greenman 			 * most of its pages taken away already, swap it out.
103411b224dcSDavid Greenman 			 */
1035ceb0cf87SJohn Dyson 			if ((action & VM_SWAP_NORMAL) ||
1036ceb0cf87SJohn Dyson 				((action & VM_SWAP_IDLE) &&
1037b40ce416SJulian Elischer 				 (minslptime > swap_idle_threshold2))) {
1038df8bae1dSRodney W. Grimes 				swapout(p);
1039df8bae1dSRodney W. Grimes 				didswap++;
10409eb881f8SSeigo Tanimura 				mtx_unlock_spin(&sched_lock);
1041664f718bSJohn Baldwin 				PROC_UNLOCK(p);
10429eb881f8SSeigo Tanimura 				vm_map_unlock(&vm->vm_map);
10439eb881f8SSeigo Tanimura 				vmspace_free(vm);
10449eb881f8SSeigo Tanimura 				sx_sunlock(&allproc_lock);
10450d94caffSDavid Greenman 				goto retry;
1046c96d52a9SJohn Baldwin 			}
1047b40ce416SJulian Elischer nextproc:
10489eb881f8SSeigo Tanimura 			mtx_unlock_spin(&sched_lock);
10498f887403SJohn Baldwin 		}
10509eb881f8SSeigo Tanimura nextproc2:
10519eb881f8SSeigo Tanimura 		PROC_UNLOCK(p);
10529eb881f8SSeigo Tanimura 		vm_map_unlock(&vm->vm_map);
10539eb881f8SSeigo Tanimura nextproc1:
10549eb881f8SSeigo Tanimura 		vmspace_free(vm);
105530171114SPeter Wemm 		continue;
1056ceb0cf87SJohn Dyson 	}
10571005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
105826f9a767SRodney W. Grimes 	/*
105926f9a767SRodney W. Grimes 	 * If we swapped something out, and another process needed memory,
106026f9a767SRodney W. Grimes 	 * then wakeup the sched process.
106126f9a767SRodney W. Grimes 	 */
10620d94caffSDavid Greenman 	if (didswap)
106324a1cce3SDavid Greenman 		wakeup(&proc0);
1064df8bae1dSRodney W. Grimes }
1065df8bae1dSRodney W. Grimes 
1066f708ef1bSPoul-Henning Kamp static void
1067df8bae1dSRodney W. Grimes swapout(p)
106854d92145SMatthew Dillon 	struct proc *p;
1069df8bae1dSRodney W. Grimes {
1070b40ce416SJulian Elischer 	struct thread *td;
1071df8bae1dSRodney W. Grimes 
1072ea754954SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
10739eb881f8SSeigo Tanimura 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1074d3a34985SJohn Dyson #if defined(SWAP_DEBUG)
1075d3a34985SJohn Dyson 	printf("swapping out %d\n", p->p_pid);
1076d3a34985SJohn Dyson #endif
10771d7b9ed2SJulian Elischer 
10781d7b9ed2SJulian Elischer 	/*
10799eb881f8SSeigo Tanimura 	 * The states of this process and its threads may have changed
10809eb881f8SSeigo Tanimura 	 * by now.  Assuming that there is only one pageout daemon thread,
10819eb881f8SSeigo Tanimura 	 * this process should still be in memory.
10829eb881f8SSeigo Tanimura 	 */
1083664f718bSJohn Baldwin 	KASSERT((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) == PS_INMEM,
10849eb881f8SSeigo Tanimura 		("swapout: lost a swapout race?"));
10859eb881f8SSeigo Tanimura 
10869eb881f8SSeigo Tanimura #if defined(INVARIANTS)
10879eb881f8SSeigo Tanimura 	/*
10881d7b9ed2SJulian Elischer 	 * Make sure that all threads are safe to be swapped out.
10891d7b9ed2SJulian Elischer 	 *
10901d7b9ed2SJulian Elischer 	 * Alternatively, we could swap out only safe threads.
10911d7b9ed2SJulian Elischer 	 */
10921d7b9ed2SJulian Elischer 	FOREACH_THREAD_IN_PROC(p, td) {
10939eb881f8SSeigo Tanimura 		KASSERT(thread_safetoswapout(td),
10949eb881f8SSeigo Tanimura 			("swapout: there is a thread not safe for swapout"));
10951d7b9ed2SJulian Elischer 	}
10969eb881f8SSeigo Tanimura #endif /* INVARIANTS */
10971d7b9ed2SJulian Elischer 
109826f9a767SRodney W. Grimes 	++p->p_stats->p_ru.ru_nswap;
1099df8bae1dSRodney W. Grimes 	/*
110026f9a767SRodney W. Grimes 	 * remember the process resident count
1101df8bae1dSRodney W. Grimes 	 */
1102b1028ad1SLuoqi Chen 	p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
1103df8bae1dSRodney W. Grimes 
11049eb881f8SSeigo Tanimura 	p->p_sflag &= ~PS_INMEM;
1105664f718bSJohn Baldwin 	p->p_sflag |= PS_SWAPPINGOUT;
1106664f718bSJohn Baldwin 	PROC_UNLOCK(p);
1107664f718bSJohn Baldwin 	FOREACH_THREAD_IN_PROC(p, td)
1108664f718bSJohn Baldwin 		TD_SET_SWAPPED(td);
11099ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
111026f9a767SRodney W. Grimes 
1111a136efe9SPeter Wemm 	vm_proc_swapout(p);
1112664f718bSJohn Baldwin 	FOREACH_THREAD_IN_PROC(p, td)
111349a2507bSAlan Cox 		vm_thread_swapout(td);
1114664f718bSJohn Baldwin 
1115664f718bSJohn Baldwin 	PROC_LOCK(p);
11169ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1117664f718bSJohn Baldwin 	p->p_sflag &= ~PS_SWAPPINGOUT;
1118df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
1119df8bae1dSRodney W. Grimes }
11205afce282SDavid Greenman #endif /* !NO_SWAPPING */
1121