160727d8bSWarner Losh /*- 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> 73da61b9a6SAlan Cox #include <sys/sched.h> 74da61b9a6SAlan Cox #include <sys/sf_buf.h> 753aa12267SBruce Evans #include <sys/shm.h> 76efeaf95aSDavid Greenman #include <sys/vmmeter.h> 771005a129SJohn Baldwin #include <sys/sx.h> 78ceb0cf87SJohn Dyson #include <sys/sysctl.h> 79df8bae1dSRodney W. Grimes 808a945d10SKonstantin Belousov #include <sys/eventhandler.h> 8126f9a767SRodney W. Grimes #include <sys/kernel.h> 820384fff8SJason Evans #include <sys/ktr.h> 83a2a1c95cSPeter Wemm #include <sys/unistd.h> 8426f9a767SRodney W. Grimes 85df8bae1dSRodney W. Grimes #include <vm/vm.h> 86efeaf95aSDavid Greenman #include <vm/vm_param.h> 87efeaf95aSDavid Greenman #include <vm/pmap.h> 88efeaf95aSDavid Greenman #include <vm/vm_map.h> 89df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9026f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 91a136efe9SPeter Wemm #include <vm/vm_object.h> 92df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 93efeaf95aSDavid Greenman #include <vm/vm_extern.h> 94a136efe9SPeter Wemm #include <vm/vm_pager.h> 9592da00bbSMatthew Dillon #include <vm/swap_pager.h> 96efeaf95aSDavid Greenman 97ea754954SJohn Baldwin extern int maxslp; 98ea754954SJohn Baldwin 992b14f991SJulian Elischer /* 1002b14f991SJulian Elischer * System initialization 1012b14f991SJulian Elischer * 1022b14f991SJulian Elischer * Note: proc0 from proc.h 1032b14f991SJulian Elischer */ 10411caded3SAlfred Perlstein static void vm_init_limits(void *); 105237fdd78SRobert Watson SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0); 1062b14f991SJulian Elischer 1072b14f991SJulian Elischer /* 1082b14f991SJulian Elischer * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 1092b14f991SJulian Elischer * 1102b14f991SJulian Elischer * Note: run scheduling should be divorced from the vm system. 1112b14f991SJulian Elischer */ 11211caded3SAlfred Perlstein static void scheduler(void *); 113237fdd78SRobert Watson SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_ANY, scheduler, NULL); 1142b14f991SJulian Elischer 115e50f5c2eSBruce Evans #ifndef NO_SWAPPING 116b61ce5b0SJeff Roberson static int swapout(struct proc *); 117b61ce5b0SJeff Roberson static void swapclear(struct proc *); 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)); 14075337a56SAlan Cox 14175337a56SAlan Cox if ((vm_offset_t)addr + len > kernel_map->max_offset || 14275337a56SAlan Cox (vm_offset_t)addr + len < (vm_offset_t)addr) 14375337a56SAlan Cox return (FALSE); 14475337a56SAlan Cox 14502c58685SPoul-Henning Kamp prot = rw; 1466cde7a16SDavid Greenman saddr = trunc_page((vm_offset_t)addr); 1476cde7a16SDavid Greenman eaddr = round_page((vm_offset_t)addr + len); 148d8834602SAlan Cox vm_map_lock_read(kernel_map); 149df8bae1dSRodney W. Grimes rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 150d8834602SAlan Cox vm_map_unlock_read(kernel_map); 151df8bae1dSRodney W. Grimes return (rv == TRUE); 152df8bae1dSRodney W. Grimes } 153df8bae1dSRodney W. Grimes 15443a90f3aSAlan Cox /* 15543a90f3aSAlan Cox * MPSAFE 1562d5c7e45SMatthew Dillon * 1572d5c7e45SMatthew Dillon * WARNING! This code calls vm_map_check_protection() which only checks 1582d5c7e45SMatthew Dillon * the associated vm_map_entry range. It does not determine whether the 1592d5c7e45SMatthew Dillon * contents of the memory is actually readable or writable. vmapbuf(), 1602d5c7e45SMatthew Dillon * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be 1612d5c7e45SMatthew Dillon * used in conjuction with this call. 16243a90f3aSAlan Cox */ 163df8bae1dSRodney W. Grimes int 164df8bae1dSRodney W. Grimes useracc(addr, len, rw) 165c3dfdfd1SAlfred Perlstein void *addr; 166df8bae1dSRodney W. Grimes int len, rw; 167df8bae1dSRodney W. Grimes { 168df8bae1dSRodney W. Grimes boolean_t rv; 16902c58685SPoul-Henning Kamp vm_prot_t prot; 17005ba50f5SJake Burkholder vm_map_t map; 171df8bae1dSRodney W. Grimes 172e50f5c2eSBruce Evans KASSERT((rw & ~VM_PROT_ALL) == 0, 17302c58685SPoul-Henning Kamp ("illegal ``rw'' argument to useracc (%x)\n", rw)); 17402c58685SPoul-Henning Kamp prot = rw; 17505ba50f5SJake Burkholder map = &curproc->p_vmspace->vm_map; 17605ba50f5SJake Burkholder if ((vm_offset_t)addr + len > vm_map_max(map) || 17705ba50f5SJake Burkholder (vm_offset_t)addr + len < (vm_offset_t)addr) { 17826f9a767SRodney W. Grimes return (FALSE); 17926f9a767SRodney W. Grimes } 180d8834602SAlan Cox vm_map_lock_read(map); 18105ba50f5SJake Burkholder rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr), 18205ba50f5SJake Burkholder round_page((vm_offset_t)addr + len), prot); 183d8834602SAlan Cox vm_map_unlock_read(map); 184df8bae1dSRodney W. Grimes return (rv == TRUE); 185df8bae1dSRodney W. Grimes } 186df8bae1dSRodney W. Grimes 18716929939SDon Lewis int 188f0ea4612SDon Lewis vslock(void *addr, size_t len) 18916929939SDon Lewis { 190bb734798SDon Lewis vm_offset_t end, last, start; 191bb734798SDon Lewis vm_size_t npages; 192bb734798SDon Lewis int error; 19316929939SDon Lewis 194bb734798SDon Lewis last = (vm_offset_t)addr + len; 195ce8660e3SDon Lewis start = trunc_page((vm_offset_t)addr); 196bb734798SDon Lewis end = round_page(last); 197bb734798SDon Lewis if (last < (vm_offset_t)addr || end < (vm_offset_t)addr) 19816929939SDon Lewis return (EINVAL); 19916929939SDon Lewis npages = atop(end - start); 20016929939SDon Lewis if (npages > vm_page_max_wired) 20116929939SDon Lewis return (ENOMEM); 202ce8660e3SDon Lewis PROC_LOCK(curproc); 203bb734798SDon Lewis if (ptoa(npages + 204bb734798SDon Lewis pmap_wired_count(vm_map_pmap(&curproc->p_vmspace->vm_map))) > 205bb734798SDon Lewis lim_cur(curproc, RLIMIT_MEMLOCK)) { 206ce8660e3SDon Lewis PROC_UNLOCK(curproc); 20716929939SDon Lewis return (ENOMEM); 20816929939SDon Lewis } 209ce8660e3SDon Lewis PROC_UNLOCK(curproc); 21016929939SDon Lewis #if 0 21116929939SDon Lewis /* 21216929939SDon Lewis * XXX - not yet 21316929939SDon Lewis * 21416929939SDon Lewis * The limit for transient usage of wired pages should be 21516929939SDon Lewis * larger than for "permanent" wired pages (mlock()). 21616929939SDon Lewis * 21716929939SDon Lewis * Also, the sysctl code, which is the only present user 21816929939SDon Lewis * of vslock(), does a hard loop on EAGAIN. 21916929939SDon Lewis */ 2202feb50bfSAttilio Rao if (npages + cnt.v_wire_count > vm_page_max_wired) 22116929939SDon Lewis return (EAGAIN); 22216929939SDon Lewis #endif 223ce8660e3SDon Lewis error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end, 224d9b2500eSBrian Feldman VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 225ce8660e3SDon Lewis /* 226ce8660e3SDon Lewis * Return EFAULT on error to match copy{in,out}() behaviour 227ce8660e3SDon Lewis * rather than returning ENOMEM like mlock() would. 228ce8660e3SDon Lewis */ 229ce8660e3SDon Lewis return (error == KERN_SUCCESS ? 0 : EFAULT); 23016929939SDon Lewis } 23116929939SDon Lewis 232ce8660e3SDon Lewis void 233f0ea4612SDon Lewis vsunlock(void *addr, size_t len) 23416929939SDon Lewis { 23516929939SDon Lewis 236ce8660e3SDon Lewis /* Rely on the parameter sanity checks performed by vslock(). */ 237ce8660e3SDon Lewis (void)vm_map_unwire(&curproc->p_vmspace->vm_map, 238ce8660e3SDon Lewis trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), 23916929939SDon Lewis VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 24016929939SDon Lewis } 24116929939SDon Lewis 242da61b9a6SAlan Cox /* 243da61b9a6SAlan Cox * Pin the page contained within the given object at the given offset. If the 244da61b9a6SAlan Cox * page is not resident, allocate and load it using the given object's pager. 245da61b9a6SAlan Cox * Return the pinned page if successful; otherwise, return NULL. 246da61b9a6SAlan Cox */ 247da61b9a6SAlan Cox static vm_page_t 248da61b9a6SAlan Cox vm_imgact_hold_page(vm_object_t object, vm_ooffset_t offset) 249da61b9a6SAlan Cox { 250da61b9a6SAlan Cox vm_page_t m, ma[1]; 251da61b9a6SAlan Cox vm_pindex_t pindex; 252da61b9a6SAlan Cox int rv; 253da61b9a6SAlan Cox 254da61b9a6SAlan Cox VM_OBJECT_LOCK(object); 255da61b9a6SAlan Cox pindex = OFF_TO_IDX(offset); 256da61b9a6SAlan Cox m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 2570a2e596aSAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 258da61b9a6SAlan Cox ma[0] = m; 259da61b9a6SAlan Cox rv = vm_pager_get_pages(object, ma, 1, 0); 260da61b9a6SAlan Cox m = vm_page_lookup(object, pindex); 261da61b9a6SAlan Cox if (m == NULL) 262da61b9a6SAlan Cox goto out; 263d1a6e42dSAlan Cox if (rv != VM_PAGER_OK) { 264da61b9a6SAlan Cox vm_page_lock_queues(); 265da61b9a6SAlan Cox vm_page_free(m); 266da61b9a6SAlan Cox vm_page_unlock_queues(); 267da61b9a6SAlan Cox m = NULL; 268da61b9a6SAlan Cox goto out; 269da61b9a6SAlan Cox } 270da61b9a6SAlan Cox } 271da61b9a6SAlan Cox vm_page_lock_queues(); 272da61b9a6SAlan Cox vm_page_hold(m); 273da61b9a6SAlan Cox vm_page_unlock_queues(); 27466bdd5d6SAlan Cox vm_page_wakeup(m); 275da61b9a6SAlan Cox out: 276da61b9a6SAlan Cox VM_OBJECT_UNLOCK(object); 277da61b9a6SAlan Cox return (m); 278da61b9a6SAlan Cox } 279da61b9a6SAlan Cox 280da61b9a6SAlan Cox /* 281da61b9a6SAlan Cox * Return a CPU private mapping to the page at the given offset within the 282da61b9a6SAlan Cox * given object. The page is pinned before it is mapped. 283da61b9a6SAlan Cox */ 284da61b9a6SAlan Cox struct sf_buf * 285da61b9a6SAlan Cox vm_imgact_map_page(vm_object_t object, vm_ooffset_t offset) 286da61b9a6SAlan Cox { 287da61b9a6SAlan Cox vm_page_t m; 288da61b9a6SAlan Cox 289da61b9a6SAlan Cox m = vm_imgact_hold_page(object, offset); 290da61b9a6SAlan Cox if (m == NULL) 291da61b9a6SAlan Cox return (NULL); 292da61b9a6SAlan Cox sched_pin(); 293da61b9a6SAlan Cox return (sf_buf_alloc(m, SFB_CPUPRIVATE)); 294da61b9a6SAlan Cox } 295da61b9a6SAlan Cox 296da61b9a6SAlan Cox /* 297da61b9a6SAlan Cox * Destroy the given CPU private mapping and unpin the page that it mapped. 298da61b9a6SAlan Cox */ 299da61b9a6SAlan Cox void 300da61b9a6SAlan Cox vm_imgact_unmap_page(struct sf_buf *sf) 301da61b9a6SAlan Cox { 302da61b9a6SAlan Cox vm_page_t m; 303da61b9a6SAlan Cox 304da61b9a6SAlan Cox m = sf_buf_page(sf); 305da61b9a6SAlan Cox sf_buf_free(sf); 306da61b9a6SAlan Cox sched_unpin(); 307da61b9a6SAlan Cox vm_page_lock_queues(); 308da61b9a6SAlan Cox vm_page_unhold(m); 309da61b9a6SAlan Cox vm_page_unlock_queues(); 310da61b9a6SAlan Cox } 311da61b9a6SAlan Cox 3128a945d10SKonstantin Belousov struct kstack_cache_entry { 3138a945d10SKonstantin Belousov vm_object_t ksobj; 3148a945d10SKonstantin Belousov struct kstack_cache_entry *next_ks_entry; 3158a945d10SKonstantin Belousov }; 3168a945d10SKonstantin Belousov 3178a945d10SKonstantin Belousov static struct kstack_cache_entry *kstack_cache; 3188a945d10SKonstantin Belousov static int kstack_cache_size = 128; 3198a945d10SKonstantin Belousov static int kstacks; 3208a945d10SKonstantin Belousov static struct mtx kstack_cache_mtx; 3218a945d10SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, kstack_cache_size, CTLFLAG_RW, &kstack_cache_size, 0, 3228a945d10SKonstantin Belousov ""); 3238a945d10SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, kstacks, CTLFLAG_RD, &kstacks, 0, 3248a945d10SKonstantin Belousov ""); 3258a945d10SKonstantin Belousov 32649a2507bSAlan Cox #ifndef KSTACK_MAX_PAGES 32749a2507bSAlan Cox #define KSTACK_MAX_PAGES 32 32849a2507bSAlan Cox #endif 32949a2507bSAlan Cox 33049a2507bSAlan Cox /* 33149a2507bSAlan Cox * Create the kernel stack (including pcb for i386) for a new thread. 33249a2507bSAlan Cox * This routine directly affects the fork perf for a process and 33349a2507bSAlan Cox * create performance for a thread. 33449a2507bSAlan Cox */ 33589b57fcfSKonstantin Belousov int 33649a2507bSAlan Cox vm_thread_new(struct thread *td, int pages) 33749a2507bSAlan Cox { 33849a2507bSAlan Cox vm_object_t ksobj; 33949a2507bSAlan Cox vm_offset_t ks; 34049a2507bSAlan Cox vm_page_t m, ma[KSTACK_MAX_PAGES]; 3418a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce; 34249a2507bSAlan Cox int i; 34349a2507bSAlan Cox 34449a2507bSAlan Cox /* Bounds check */ 34549a2507bSAlan Cox if (pages <= 1) 34649a2507bSAlan Cox pages = KSTACK_PAGES; 34749a2507bSAlan Cox else if (pages > KSTACK_MAX_PAGES) 34849a2507bSAlan Cox pages = KSTACK_MAX_PAGES; 3498a945d10SKonstantin Belousov 3508a945d10SKonstantin Belousov if (pages == KSTACK_PAGES) { 3518a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 3528a945d10SKonstantin Belousov if (kstack_cache != NULL) { 3538a945d10SKonstantin Belousov ks_ce = kstack_cache; 3548a945d10SKonstantin Belousov kstack_cache = ks_ce->next_ks_entry; 3558a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 3568a945d10SKonstantin Belousov 3578a945d10SKonstantin Belousov td->td_kstack_obj = ks_ce->ksobj; 3588a945d10SKonstantin Belousov td->td_kstack = (vm_offset_t)ks_ce; 3598a945d10SKonstantin Belousov td->td_kstack_pages = KSTACK_PAGES; 3608a945d10SKonstantin Belousov return (1); 3618a945d10SKonstantin Belousov } 3628a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 3638a945d10SKonstantin Belousov } 3648a945d10SKonstantin Belousov 36549a2507bSAlan Cox /* 36649a2507bSAlan Cox * Allocate an object for the kstack. 36749a2507bSAlan Cox */ 36849a2507bSAlan Cox ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 369374ae2a3SJeff Roberson 37049a2507bSAlan Cox /* 37149a2507bSAlan Cox * Get a kernel virtual address for this thread's kstack. 37249a2507bSAlan Cox */ 37349a2507bSAlan Cox ks = kmem_alloc_nofault(kernel_map, 37449a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 37589b57fcfSKonstantin Belousov if (ks == 0) { 37689b57fcfSKonstantin Belousov printf("vm_thread_new: kstack allocation failed\n"); 37789b57fcfSKonstantin Belousov vm_object_deallocate(ksobj); 37889b57fcfSKonstantin Belousov return (0); 37989b57fcfSKonstantin Belousov } 38089b57fcfSKonstantin Belousov 3818a945d10SKonstantin Belousov atomic_add_int(&kstacks, 1); 38249a2507bSAlan Cox if (KSTACK_GUARD_PAGES != 0) { 38349a2507bSAlan Cox pmap_qremove(ks, KSTACK_GUARD_PAGES); 38449a2507bSAlan Cox ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 38549a2507bSAlan Cox } 38689b57fcfSKonstantin Belousov td->td_kstack_obj = ksobj; 38749a2507bSAlan Cox td->td_kstack = ks; 38849a2507bSAlan Cox /* 38949a2507bSAlan Cox * Knowing the number of pages allocated is useful when you 39049a2507bSAlan Cox * want to deallocate them. 39149a2507bSAlan Cox */ 39249a2507bSAlan Cox td->td_kstack_pages = pages; 39349a2507bSAlan Cox /* 39449a2507bSAlan Cox * For the length of the stack, link in a real page of ram for each 39549a2507bSAlan Cox * page of stack. 39649a2507bSAlan Cox */ 39749a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 39849a2507bSAlan Cox for (i = 0; i < pages; i++) { 39949a2507bSAlan Cox /* 40049a2507bSAlan Cox * Get a kernel stack page. 40149a2507bSAlan Cox */ 402ddf4bb37SAlan Cox m = vm_page_grab(ksobj, i, VM_ALLOC_NOBUSY | 40349a2507bSAlan Cox VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED); 40449a2507bSAlan Cox ma[i] = m; 40549a2507bSAlan Cox m->valid = VM_PAGE_BITS_ALL; 40649a2507bSAlan Cox } 40749a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 40849a2507bSAlan Cox pmap_qenter(ks, ma, pages); 40989b57fcfSKonstantin Belousov return (1); 41049a2507bSAlan Cox } 41149a2507bSAlan Cox 4128a945d10SKonstantin Belousov static void 4138a945d10SKonstantin Belousov vm_thread_stack_dispose(vm_object_t ksobj, vm_offset_t ks, int pages) 41449a2507bSAlan Cox { 41549a2507bSAlan Cox vm_page_t m; 4168a945d10SKonstantin Belousov int i; 41749a2507bSAlan Cox 4188a945d10SKonstantin Belousov atomic_add_int(&kstacks, -1); 41949a2507bSAlan Cox pmap_qremove(ks, pages); 42049a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 42149a2507bSAlan Cox for (i = 0; i < pages; i++) { 42249a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 42349a2507bSAlan Cox if (m == NULL) 42449a2507bSAlan Cox panic("vm_thread_dispose: kstack already missing?"); 42549a2507bSAlan Cox vm_page_lock_queues(); 42649a2507bSAlan Cox vm_page_unwire(m, 0); 42749a2507bSAlan Cox vm_page_free(m); 42849a2507bSAlan Cox vm_page_unlock_queues(); 42949a2507bSAlan Cox } 43049a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 43149a2507bSAlan Cox vm_object_deallocate(ksobj); 43249a2507bSAlan Cox kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 43349a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 434c3cf0b47SKonstantin Belousov } 435c3cf0b47SKonstantin Belousov 436c3cf0b47SKonstantin Belousov /* 4378a945d10SKonstantin Belousov * Dispose of a thread's kernel stack. 4388a945d10SKonstantin Belousov */ 4398a945d10SKonstantin Belousov void 4408a945d10SKonstantin Belousov vm_thread_dispose(struct thread *td) 4418a945d10SKonstantin Belousov { 4428a945d10SKonstantin Belousov vm_object_t ksobj; 4438a945d10SKonstantin Belousov vm_offset_t ks; 4448a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce; 4458a945d10SKonstantin Belousov int pages; 4468a945d10SKonstantin Belousov 4478a945d10SKonstantin Belousov pages = td->td_kstack_pages; 4488a945d10SKonstantin Belousov ksobj = td->td_kstack_obj; 4498a945d10SKonstantin Belousov ks = td->td_kstack; 4508a945d10SKonstantin Belousov td->td_kstack = 0; 4518a945d10SKonstantin Belousov td->td_kstack_pages = 0; 4528a945d10SKonstantin Belousov if (pages == KSTACK_PAGES && kstacks <= kstack_cache_size) { 4538a945d10SKonstantin Belousov ks_ce = (struct kstack_cache_entry *)ks; 4548a945d10SKonstantin Belousov ks_ce->ksobj = ksobj; 4558a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 4568a945d10SKonstantin Belousov ks_ce->next_ks_entry = kstack_cache; 4578a945d10SKonstantin Belousov kstack_cache = ks_ce; 4588a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 4598a945d10SKonstantin Belousov return; 4608a945d10SKonstantin Belousov } 4618a945d10SKonstantin Belousov vm_thread_stack_dispose(ksobj, ks, pages); 4628a945d10SKonstantin Belousov } 4638a945d10SKonstantin Belousov 4648a945d10SKonstantin Belousov static void 4658a945d10SKonstantin Belousov vm_thread_stack_lowmem(void *nulll) 4668a945d10SKonstantin Belousov { 4678a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce, *ks_ce1; 4688a945d10SKonstantin Belousov 4698a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 4708a945d10SKonstantin Belousov ks_ce = kstack_cache; 4718a945d10SKonstantin Belousov kstack_cache = NULL; 4728a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 4738a945d10SKonstantin Belousov 4748a945d10SKonstantin Belousov while (ks_ce != NULL) { 4758a945d10SKonstantin Belousov ks_ce1 = ks_ce; 4768a945d10SKonstantin Belousov ks_ce = ks_ce->next_ks_entry; 4778a945d10SKonstantin Belousov 4788a945d10SKonstantin Belousov vm_thread_stack_dispose(ks_ce1->ksobj, (vm_offset_t)ks_ce1, 4798a945d10SKonstantin Belousov KSTACK_PAGES); 4808a945d10SKonstantin Belousov } 4818a945d10SKonstantin Belousov } 4828a945d10SKonstantin Belousov 4838a945d10SKonstantin Belousov static void 4848a945d10SKonstantin Belousov kstack_cache_init(void *nulll) 4858a945d10SKonstantin Belousov { 4868a945d10SKonstantin Belousov 4878a945d10SKonstantin Belousov EVENTHANDLER_REGISTER(vm_lowmem, vm_thread_stack_lowmem, NULL, 4888a945d10SKonstantin Belousov EVENTHANDLER_PRI_ANY); 4898a945d10SKonstantin Belousov } 4908a945d10SKonstantin Belousov 4918a945d10SKonstantin Belousov MTX_SYSINIT(kstack_cache, &kstack_cache_mtx, "kstkch", MTX_DEF); 4928a945d10SKonstantin Belousov SYSINIT(vm_kstacks, SI_SUB_KTHREAD_INIT, SI_ORDER_ANY, kstack_cache_init, NULL); 4938a945d10SKonstantin Belousov 4948a945d10SKonstantin Belousov /* 49549a2507bSAlan Cox * Allow a thread's kernel stack to be paged out. 49649a2507bSAlan Cox */ 49749a2507bSAlan Cox void 49849a2507bSAlan Cox vm_thread_swapout(struct thread *td) 49949a2507bSAlan Cox { 50049a2507bSAlan Cox vm_object_t ksobj; 50149a2507bSAlan Cox vm_page_t m; 50249a2507bSAlan Cox int i, pages; 50349a2507bSAlan Cox 504710338e9SMarcel Moolenaar cpu_thread_swapout(td); 50549a2507bSAlan Cox pages = td->td_kstack_pages; 50649a2507bSAlan Cox ksobj = td->td_kstack_obj; 50749a2507bSAlan Cox pmap_qremove(td->td_kstack, pages); 50849a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 50949a2507bSAlan Cox for (i = 0; i < pages; i++) { 51049a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 51149a2507bSAlan Cox if (m == NULL) 51249a2507bSAlan Cox panic("vm_thread_swapout: kstack already missing?"); 51349a2507bSAlan Cox vm_page_lock_queues(); 51449a2507bSAlan Cox vm_page_dirty(m); 51549a2507bSAlan Cox vm_page_unwire(m, 0); 51649a2507bSAlan Cox vm_page_unlock_queues(); 51749a2507bSAlan Cox } 51849a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 51949a2507bSAlan Cox } 52049a2507bSAlan Cox 52149a2507bSAlan Cox /* 52249a2507bSAlan Cox * Bring the kernel stack for a specified thread back in. 52349a2507bSAlan Cox */ 52449a2507bSAlan Cox void 52549a2507bSAlan Cox vm_thread_swapin(struct thread *td) 52649a2507bSAlan Cox { 52749a2507bSAlan Cox vm_object_t ksobj; 52849a2507bSAlan Cox vm_page_t m, ma[KSTACK_MAX_PAGES]; 52949a2507bSAlan Cox int i, pages, rv; 53049a2507bSAlan Cox 53149a2507bSAlan Cox pages = td->td_kstack_pages; 53249a2507bSAlan Cox ksobj = td->td_kstack_obj; 53349a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 53449a2507bSAlan Cox for (i = 0; i < pages; i++) { 53549a2507bSAlan Cox m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 53649a2507bSAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 53749a2507bSAlan Cox rv = vm_pager_get_pages(ksobj, &m, 1, 0); 53849a2507bSAlan Cox if (rv != VM_PAGER_OK) 53949a2507bSAlan Cox panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid); 54049a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 54149a2507bSAlan Cox } 54249a2507bSAlan Cox ma[i] = m; 54349a2507bSAlan Cox vm_page_lock_queues(); 54449a2507bSAlan Cox vm_page_wire(m); 54549a2507bSAlan Cox vm_page_unlock_queues(); 54666bdd5d6SAlan Cox vm_page_wakeup(m); 54749a2507bSAlan Cox } 54849a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 54949a2507bSAlan Cox pmap_qenter(td->td_kstack, ma, pages); 550710338e9SMarcel Moolenaar cpu_thread_swapin(td); 55149a2507bSAlan Cox } 55249a2507bSAlan Cox 553a136efe9SPeter Wemm /* 554df8bae1dSRodney W. Grimes * Implement fork's actions on an address space. 555df8bae1dSRodney W. Grimes * Here we arrange for the address space to be copied or referenced, 556df8bae1dSRodney W. Grimes * allocate a user struct (pcb and kernel stack), then call the 557df8bae1dSRodney W. Grimes * machine-dependent layer to fill those in and make the new process 558a2a1c95cSPeter Wemm * ready to run. The new process is set up so that it returns directly 559a2a1c95cSPeter Wemm * to user mode to avoid stack copying and relocation problems. 560df8bae1dSRodney W. Grimes */ 56189b57fcfSKonstantin Belousov int 56289b57fcfSKonstantin Belousov vm_forkproc(td, p2, td2, vm2, flags) 563b40ce416SJulian Elischer struct thread *td; 564b40ce416SJulian Elischer struct proc *p2; 565079b7badSJulian Elischer struct thread *td2; 56689b57fcfSKonstantin Belousov struct vmspace *vm2; 567a2a1c95cSPeter Wemm int flags; 568df8bae1dSRodney W. Grimes { 569b40ce416SJulian Elischer struct proc *p1 = td->td_proc; 57089b57fcfSKonstantin Belousov int error; 571df8bae1dSRodney W. Grimes 57291c28bfdSLuoqi Chen if ((flags & RFPROC) == 0) { 57391c28bfdSLuoqi Chen /* 57491c28bfdSLuoqi Chen * Divorce the memory, if it is shared, essentially 57591c28bfdSLuoqi Chen * this changes shared memory amongst threads, into 57691c28bfdSLuoqi Chen * COW locally. 57791c28bfdSLuoqi Chen */ 57891c28bfdSLuoqi Chen if ((flags & RFMEM) == 0) { 57991c28bfdSLuoqi Chen if (p1->p_vmspace->vm_refcnt > 1) { 58089b57fcfSKonstantin Belousov error = vmspace_unshare(p1); 58189b57fcfSKonstantin Belousov if (error) 58289b57fcfSKonstantin Belousov return (error); 58391c28bfdSLuoqi Chen } 58491c28bfdSLuoqi Chen } 585079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 58689b57fcfSKonstantin Belousov return (0); 58791c28bfdSLuoqi Chen } 58891c28bfdSLuoqi Chen 5895856e12eSJohn Dyson if (flags & RFMEM) { 5905856e12eSJohn Dyson p2->p_vmspace = p1->p_vmspace; 5911a276a3fSAlan Cox atomic_add_int(&p1->p_vmspace->vm_refcnt, 1); 5925856e12eSJohn Dyson } 5935856e12eSJohn Dyson 59490ecac61SMatthew Dillon while (vm_page_count_severe()) { 59526f9a767SRodney W. Grimes VM_WAIT; 5960d94caffSDavid Greenman } 59726f9a767SRodney W. Grimes 5985856e12eSJohn Dyson if ((flags & RFMEM) == 0) { 59989b57fcfSKonstantin Belousov p2->p_vmspace = vm2; 600df8bae1dSRodney W. Grimes if (p1->p_vmspace->vm_shm) 601dabee6feSPeter Wemm shmfork(p1, p2); 602a2a1c95cSPeter Wemm } 603df8bae1dSRodney W. Grimes 60439fb8e6bSJulian Elischer /* 605a2a1c95cSPeter Wemm * cpu_fork will copy and update the pcb, set up the kernel stack, 606a2a1c95cSPeter Wemm * and make the child ready to run. 607df8bae1dSRodney W. Grimes */ 608079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 60989b57fcfSKonstantin Belousov return (0); 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes 612df8bae1dSRodney W. Grimes /* 613eb30c1c0SPeter Wemm * Called after process has been wait(2)'ed apon and is being reaped. 614eb30c1c0SPeter Wemm * The idea is to reclaim resources that we could not reclaim while 615eb30c1c0SPeter Wemm * the process was still executing. 616eb30c1c0SPeter Wemm */ 617eb30c1c0SPeter Wemm void 618eb30c1c0SPeter Wemm vm_waitproc(p) 619eb30c1c0SPeter Wemm struct proc *p; 620eb30c1c0SPeter Wemm { 621eb30c1c0SPeter Wemm 622582ec34cSAlfred Perlstein vmspace_exitfree(p); /* and clean-out the vmspace */ 623eb30c1c0SPeter Wemm } 624eb30c1c0SPeter Wemm 625eb30c1c0SPeter Wemm /* 626df8bae1dSRodney W. Grimes * Set default limits for VM system. 627df8bae1dSRodney W. Grimes * Called for proc 0, and then inherited by all others. 6282b14f991SJulian Elischer * 6292b14f991SJulian Elischer * XXX should probably act directly on proc0. 630df8bae1dSRodney W. Grimes */ 6312b14f991SJulian Elischer static void 6322b14f991SJulian Elischer vm_init_limits(udata) 6334590fd3aSDavid Greenman void *udata; 634df8bae1dSRodney W. Grimes { 63554d92145SMatthew Dillon struct proc *p = udata; 63691d5354aSJohn Baldwin struct plimit *limp; 637bbc0ec52SDavid Greenman int rss_limit; 638df8bae1dSRodney W. Grimes 639df8bae1dSRodney W. Grimes /* 6400d94caffSDavid Greenman * Set up the initial limits on process VM. Set the maximum resident 6410d94caffSDavid Greenman * set size to be half of (reasonably) available memory. Since this 6420d94caffSDavid Greenman * is a soft limit, it comes into effect only when the system is out 6430d94caffSDavid Greenman * of memory - half of main memory helps to favor smaller processes, 644bbc0ec52SDavid Greenman * and reduces thrashing of the object cache. 645df8bae1dSRodney W. Grimes */ 64691d5354aSJohn Baldwin limp = p->p_limit; 64791d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 64891d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 64991d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 65091d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 651dd0bd066SDavid Greenman /* limit the limit to no less than 2MB */ 6522feb50bfSAttilio Rao rss_limit = max(cnt.v_free_count, 512); 65391d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 65491d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes 65726f9a767SRodney W. Grimes void 65826f9a767SRodney W. Grimes faultin(p) 65926f9a767SRodney W. Grimes struct proc *p; 66026f9a767SRodney W. Grimes { 66111edc1e0SJohn Baldwin #ifdef NO_SWAPPING 66211edc1e0SJohn Baldwin 66311edc1e0SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 664b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 66511edc1e0SJohn Baldwin panic("faultin: proc swapped out with NO_SWAPPING!"); 66611edc1e0SJohn Baldwin #else /* !NO_SWAPPING */ 667664f718bSJohn Baldwin struct thread *td; 66826f9a767SRodney W. Grimes 669c96d52a9SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 6701d7b9ed2SJulian Elischer /* 6711d7b9ed2SJulian Elischer * If another process is swapping in this process, 6721d7b9ed2SJulian Elischer * just wait until it finishes. 6731d7b9ed2SJulian Elischer */ 674b61ce5b0SJeff Roberson if (p->p_flag & P_SWAPPINGIN) { 675b61ce5b0SJeff Roberson while (p->p_flag & P_SWAPPINGIN) 676b61ce5b0SJeff Roberson msleep(&p->p_flag, &p->p_mtx, PVM, "faultin", 0); 677b61ce5b0SJeff Roberson return; 678b61ce5b0SJeff Roberson } 679b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) { 680664f718bSJohn Baldwin /* 681664f718bSJohn Baldwin * Don't let another thread swap process p out while we are 682664f718bSJohn Baldwin * busy swapping it in. 683664f718bSJohn Baldwin */ 684664f718bSJohn Baldwin ++p->p_lock; 685b61ce5b0SJeff Roberson p->p_flag |= P_SWAPPINGIN; 68645ece682SJohn Baldwin PROC_UNLOCK(p); 68726f9a767SRodney W. Grimes 688b61ce5b0SJeff Roberson /* 689b61ce5b0SJeff Roberson * We hold no lock here because the list of threads 690b61ce5b0SJeff Roberson * can not change while all threads in the process are 691b61ce5b0SJeff Roberson * swapped out. 692b61ce5b0SJeff Roberson */ 693664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) 69449a2507bSAlan Cox vm_thread_swapin(td); 69545ece682SJohn Baldwin PROC_LOCK(p); 696b61ce5b0SJeff Roberson swapclear(p); 697258853abSJeff Roberson p->p_swtick = ticks; 69826f9a767SRodney W. Grimes 699b61ce5b0SJeff Roberson wakeup(&p->p_flag); 70026f9a767SRodney W. Grimes 701664f718bSJohn Baldwin /* Allow other threads to swap p out now. */ 70226f9a767SRodney W. Grimes --p->p_lock; 70326f9a767SRodney W. Grimes } 70411edc1e0SJohn Baldwin #endif /* NO_SWAPPING */ 70526f9a767SRodney W. Grimes } 70626f9a767SRodney W. Grimes 707df8bae1dSRodney W. Grimes /* 70826f9a767SRodney W. Grimes * This swapin algorithm attempts to swap-in processes only if there 70926f9a767SRodney W. Grimes * is enough space for them. Of course, if a process waits for a long 71026f9a767SRodney W. Grimes * time, it will be swapped in anyway. 7110384fff8SJason Evans * 71210c447faSAlan Cox * Giant is held on entry. 713df8bae1dSRodney W. Grimes */ 7142b14f991SJulian Elischer /* ARGSUSED*/ 7152b14f991SJulian Elischer static void 716d841aaa7SBruce Evans scheduler(dummy) 717d841aaa7SBruce Evans void *dummy; 718df8bae1dSRodney W. Grimes { 71954d92145SMatthew Dillon struct proc *p; 720e602ba25SJulian Elischer struct thread *td; 721df8bae1dSRodney W. Grimes struct proc *pp; 722258853abSJeff Roberson int slptime; 723258853abSJeff Roberson int swtime; 724df8bae1dSRodney W. Grimes int ppri; 725258853abSJeff Roberson int pri; 726df8bae1dSRodney W. Grimes 727c96d52a9SJohn Baldwin mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 72810c447faSAlan Cox mtx_unlock(&Giant); 7290384fff8SJason Evans 730df8bae1dSRodney W. Grimes loop: 73190ecac61SMatthew Dillon if (vm_page_count_min()) { 7320d94caffSDavid Greenman VM_WAIT; 73390ecac61SMatthew Dillon goto loop; 7340d94caffSDavid Greenman } 73526f9a767SRodney W. Grimes 736df8bae1dSRodney W. Grimes pp = NULL; 737df8bae1dSRodney W. Grimes ppri = INT_MIN; 7381005a129SJohn Baldwin sx_slock(&allproc_lock); 739b40ce416SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 740b61ce5b0SJeff Roberson PROC_LOCK(p); 741b61ce5b0SJeff Roberson if (p->p_flag & (P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) { 742b61ce5b0SJeff Roberson PROC_UNLOCK(p); 743e602ba25SJulian Elischer continue; 744e602ba25SJulian Elischer } 745258853abSJeff Roberson swtime = (ticks - p->p_swtick) / hz; 746e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 7471d7b9ed2SJulian Elischer /* 74871fad9fdSJulian Elischer * An otherwise runnable thread of a process 74971fad9fdSJulian Elischer * swapped out has only the TDI_SWAPPED bit set. 75071fad9fdSJulian Elischer * 7511d7b9ed2SJulian Elischer */ 752982d11f8SJeff Roberson thread_lock(td); 75371fad9fdSJulian Elischer if (td->td_inhibitors == TDI_SWAPPED) { 754258853abSJeff Roberson slptime = (ticks - td->td_slptick) / hz; 755258853abSJeff Roberson pri = swtime + slptime; 756b61ce5b0SJeff Roberson if ((td->td_flags & TDF_SWAPINREQ) == 0) 757fa885116SJulian Elischer pri -= p->p_nice * 8; 75826f9a767SRodney W. Grimes /* 759ad1e7d28SJulian Elischer * if this thread is higher priority 760b40ce416SJulian Elischer * and there is enough space, then select 761b40ce416SJulian Elischer * this process instead of the previous 762b40ce416SJulian Elischer * selection. 76326f9a767SRodney W. Grimes */ 7640d94caffSDavid Greenman if (pri > ppri) { 765df8bae1dSRodney W. Grimes pp = p; 766df8bae1dSRodney W. Grimes ppri = pri; 767df8bae1dSRodney W. Grimes } 768df8bae1dSRodney W. Grimes } 769982d11f8SJeff Roberson thread_unlock(td); 770b40ce416SJulian Elischer } 771b61ce5b0SJeff Roberson PROC_UNLOCK(p); 772df8bae1dSRodney W. Grimes } 7731005a129SJohn Baldwin sx_sunlock(&allproc_lock); 77426f9a767SRodney W. Grimes 775df8bae1dSRodney W. Grimes /* 776a669a6e9SJohn Dyson * Nothing to do, back to sleep. 777df8bae1dSRodney W. Grimes */ 778df8bae1dSRodney W. Grimes if ((p = pp) == NULL) { 779da7bbd2cSJohn Baldwin tsleep(&proc0, PVM, "sched", maxslp * hz / 2); 780df8bae1dSRodney W. Grimes goto loop; 781df8bae1dSRodney W. Grimes } 7821d7b9ed2SJulian Elischer PROC_LOCK(p); 7831d7b9ed2SJulian Elischer 7841d7b9ed2SJulian Elischer /* 7851d7b9ed2SJulian Elischer * Another process may be bringing or may have already 7861d7b9ed2SJulian Elischer * brought this process in while we traverse all threads. 7871d7b9ed2SJulian Elischer * Or, this process may even be being swapped out again. 7881d7b9ed2SJulian Elischer */ 789b61ce5b0SJeff Roberson if (p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) { 7901d7b9ed2SJulian Elischer PROC_UNLOCK(p); 7911d7b9ed2SJulian Elischer goto loop; 7921d7b9ed2SJulian Elischer } 7931d7b9ed2SJulian Elischer 794df8bae1dSRodney W. Grimes /* 79526f9a767SRodney W. Grimes * We would like to bring someone in. (only if there is space). 796e602ba25SJulian Elischer * [What checks the space? ] 797df8bae1dSRodney W. Grimes */ 79826f9a767SRodney W. Grimes faultin(p); 79945ece682SJohn Baldwin PROC_UNLOCK(p); 800df8bae1dSRodney W. Grimes goto loop; 801df8bae1dSRodney W. Grimes } 802df8bae1dSRodney W. Grimes 803da7bbd2cSJohn Baldwin void 804da7bbd2cSJohn Baldwin kick_proc0(void) 805d13ec713SStephan Uphoff { 806d13ec713SStephan Uphoff 807da7bbd2cSJohn Baldwin wakeup(&proc0); 808d13ec713SStephan Uphoff } 809d13ec713SStephan Uphoff 8105afce282SDavid Greenman #ifndef NO_SWAPPING 8115afce282SDavid Greenman 812ceb0cf87SJohn Dyson /* 813ceb0cf87SJohn Dyson * Swap_idle_threshold1 is the guaranteed swapped in time for a process 814ceb0cf87SJohn Dyson */ 815303b270bSEivind Eklund static int swap_idle_threshold1 = 2; 8162a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW, 8179faaf3b3STom Rhodes &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process"); 818ceb0cf87SJohn Dyson 819ceb0cf87SJohn Dyson /* 820ceb0cf87SJohn Dyson * Swap_idle_threshold2 is the time that a process can be idle before 821ceb0cf87SJohn Dyson * it will be swapped out, if idle swapping is enabled. 822ceb0cf87SJohn Dyson */ 823303b270bSEivind Eklund static int swap_idle_threshold2 = 10; 8242a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW, 8259faaf3b3STom Rhodes &swap_idle_threshold2, 0, "Time before a process will be swapped out"); 826ceb0cf87SJohn Dyson 827df8bae1dSRodney W. Grimes /* 828df8bae1dSRodney W. Grimes * Swapout is driven by the pageout daemon. Very simple, we find eligible 829b61ce5b0SJeff Roberson * procs and swap out their stacks. We try to always "swap" at least one 830df8bae1dSRodney W. Grimes * process in case we need the room for a swapin. 831df8bae1dSRodney W. Grimes * If any procs have been sleeping/stopped for at least maxslp seconds, 832df8bae1dSRodney W. Grimes * they are swapped. Else, we swap the longest-sleeping or stopped process, 833df8bae1dSRodney W. Grimes * if any, otherwise the longest-resident process. 834df8bae1dSRodney W. Grimes */ 835df8bae1dSRodney W. Grimes void 8363a2dc656SJohn Dyson swapout_procs(action) 8373a2dc656SJohn Dyson int action; 838df8bae1dSRodney W. Grimes { 83954d92145SMatthew Dillon struct proc *p; 840e602ba25SJulian Elischer struct thread *td; 841df8bae1dSRodney W. Grimes int didswap = 0; 842df8bae1dSRodney W. Grimes 8430d94caffSDavid Greenman retry: 8443a2189d4SJohn Baldwin sx_slock(&allproc_lock); 845e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 846b18bfc3dSJohn Dyson struct vmspace *vm; 847b40ce416SJulian Elischer int minslptime = 100000; 848258853abSJeff Roberson int slptime; 849b18bfc3dSJohn Dyson 8509eb881f8SSeigo Tanimura /* 851b1f99ebeSSeigo Tanimura * Watch out for a process in 852b1f99ebeSSeigo Tanimura * creation. It may have no 8531c865ac7SJohn Baldwin * address space or lock yet. 8541c865ac7SJohn Baldwin */ 855b61ce5b0SJeff Roberson if (p->p_state == PRS_NEW) 8561c865ac7SJohn Baldwin continue; 8571c865ac7SJohn Baldwin /* 858b1f99ebeSSeigo Tanimura * An aio daemon switches its 859b1f99ebeSSeigo Tanimura * address space while running. 860b1f99ebeSSeigo Tanimura * Perform a quick check whether 861b1f99ebeSSeigo Tanimura * a process has P_SYSTEM. 8629eb881f8SSeigo Tanimura */ 8638f887403SJohn Baldwin if ((p->p_flag & P_SYSTEM) != 0) 864b1f99ebeSSeigo Tanimura continue; 8651c865ac7SJohn Baldwin /* 8661c865ac7SJohn Baldwin * Do not swapout a process that 8671c865ac7SJohn Baldwin * is waiting for VM data 8681c865ac7SJohn Baldwin * structures as there is a possible 8691c865ac7SJohn Baldwin * deadlock. Test this first as 8701c865ac7SJohn Baldwin * this may block. 8711c865ac7SJohn Baldwin * 8721c865ac7SJohn Baldwin * Lock the map until swapout 8731c865ac7SJohn Baldwin * finishes, or a thread of this 8741c865ac7SJohn Baldwin * process may attempt to alter 8751c865ac7SJohn Baldwin * the map. 8761c865ac7SJohn Baldwin */ 87757051fdcSTor Egge vm = vmspace_acquire_ref(p); 87857051fdcSTor Egge if (vm == NULL) 87957051fdcSTor Egge continue; 8809eb881f8SSeigo Tanimura if (!vm_map_trylock(&vm->vm_map)) 8819eb881f8SSeigo Tanimura goto nextproc1; 8829eb881f8SSeigo Tanimura 8835074aecdSJohn Baldwin PROC_LOCK(p); 88469b40456SJohn Baldwin if (p->p_lock != 0 || 8851279572aSDavid Xu (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT) 8861279572aSDavid Xu ) != 0) { 887374ae2a3SJeff Roberson goto nextproc; 8885074aecdSJohn Baldwin } 88923955314SAlfred Perlstein /* 89023955314SAlfred Perlstein * only aiod changes vmspace, however it will be 89123955314SAlfred Perlstein * skipped because of the if statement above checking 89223955314SAlfred Perlstein * for P_SYSTEM 89323955314SAlfred Perlstein */ 894b61ce5b0SJeff Roberson if ((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) != P_INMEM) 895374ae2a3SJeff Roberson goto nextproc; 89669b40456SJohn Baldwin 897e602ba25SJulian Elischer switch (p->p_state) { 8980d94caffSDavid Greenman default: 899e602ba25SJulian Elischer /* Don't swap out processes in any sort 900e602ba25SJulian Elischer * of 'special' state. */ 9018f887403SJohn Baldwin break; 902df8bae1dSRodney W. Grimes 903e602ba25SJulian Elischer case PRS_NORMAL: 90426f9a767SRodney W. Grimes /* 905bfbfac11SDavid Greenman * do not swapout a realtime process 906b40ce416SJulian Elischer * Check all the thread groups.. 907bfbfac11SDavid Greenman */ 9088460a577SJohn Birrell FOREACH_THREAD_IN_PROC(p, td) { 909b61ce5b0SJeff Roberson thread_lock(td); 910b61ce5b0SJeff Roberson if (PRI_IS_REALTIME(td->td_pri_class)) { 911b61ce5b0SJeff Roberson thread_unlock(td); 912b40ce416SJulian Elischer goto nextproc; 913b61ce5b0SJeff Roberson } 914258853abSJeff Roberson slptime = (ticks - td->td_slptick) / hz; 915bfbfac11SDavid Greenman /* 9169eb881f8SSeigo Tanimura * Guarantee swap_idle_threshold1 917ceb0cf87SJohn Dyson * time in memory. 9180d94caffSDavid Greenman */ 919258853abSJeff Roberson if (slptime < swap_idle_threshold1) { 920b61ce5b0SJeff Roberson thread_unlock(td); 921b40ce416SJulian Elischer goto nextproc; 922b61ce5b0SJeff Roberson } 9239eb881f8SSeigo Tanimura 9241d7b9ed2SJulian Elischer /* 9259eb881f8SSeigo Tanimura * Do not swapout a process if it is 9269eb881f8SSeigo Tanimura * waiting on a critical event of some 9279eb881f8SSeigo Tanimura * kind or there is a thread whose 9289eb881f8SSeigo Tanimura * pageable memory may be accessed. 9291d7b9ed2SJulian Elischer * 9301d7b9ed2SJulian Elischer * This could be refined to support 9311d7b9ed2SJulian Elischer * swapping out a thread. 9321d7b9ed2SJulian Elischer */ 933c5aa6b58SJeff Roberson if (!thread_safetoswapout(td)) { 934b61ce5b0SJeff Roberson thread_unlock(td); 935e602ba25SJulian Elischer goto nextproc; 936b61ce5b0SJeff Roberson } 937ceb0cf87SJohn Dyson /* 938b40ce416SJulian Elischer * If the system is under memory stress, 939b40ce416SJulian Elischer * or if we are swapping 940b40ce416SJulian Elischer * idle processes >= swap_idle_threshold2, 941b40ce416SJulian Elischer * then swap the process out. 942ceb0cf87SJohn Dyson */ 943ceb0cf87SJohn Dyson if (((action & VM_SWAP_NORMAL) == 0) && 944ceb0cf87SJohn Dyson (((action & VM_SWAP_IDLE) == 0) || 945258853abSJeff Roberson (slptime < swap_idle_threshold2))) { 946b61ce5b0SJeff Roberson thread_unlock(td); 947b40ce416SJulian Elischer goto nextproc; 948b61ce5b0SJeff Roberson } 9499eb881f8SSeigo Tanimura 950258853abSJeff Roberson if (minslptime > slptime) 951258853abSJeff Roberson minslptime = slptime; 952b61ce5b0SJeff Roberson thread_unlock(td); 953b40ce416SJulian Elischer } 9540d94caffSDavid Greenman 95511b224dcSDavid Greenman /* 95617d9d0d0SDavid Schultz * If the pageout daemon didn't free enough pages, 95717d9d0d0SDavid Schultz * or if this process is idle and the system is 95817d9d0d0SDavid Schultz * configured to swap proactively, swap it out. 95911b224dcSDavid Greenman */ 960ceb0cf87SJohn Dyson if ((action & VM_SWAP_NORMAL) || 961ceb0cf87SJohn Dyson ((action & VM_SWAP_IDLE) && 962b40ce416SJulian Elischer (minslptime > swap_idle_threshold2))) { 963b61ce5b0SJeff Roberson if (swapout(p) == 0) 964df8bae1dSRodney W. Grimes didswap++; 965664f718bSJohn Baldwin PROC_UNLOCK(p); 9669eb881f8SSeigo Tanimura vm_map_unlock(&vm->vm_map); 9679eb881f8SSeigo Tanimura vmspace_free(vm); 9689eb881f8SSeigo Tanimura sx_sunlock(&allproc_lock); 9690d94caffSDavid Greenman goto retry; 970c96d52a9SJohn Baldwin } 9718f887403SJohn Baldwin } 972374ae2a3SJeff Roberson nextproc: 9739eb881f8SSeigo Tanimura PROC_UNLOCK(p); 9749eb881f8SSeigo Tanimura vm_map_unlock(&vm->vm_map); 9759eb881f8SSeigo Tanimura nextproc1: 9769eb881f8SSeigo Tanimura vmspace_free(vm); 97730171114SPeter Wemm continue; 978ceb0cf87SJohn Dyson } 9791005a129SJohn Baldwin sx_sunlock(&allproc_lock); 98026f9a767SRodney W. Grimes /* 98126f9a767SRodney W. Grimes * If we swapped something out, and another process needed memory, 98226f9a767SRodney W. Grimes * then wakeup the sched process. 98326f9a767SRodney W. Grimes */ 9840d94caffSDavid Greenman if (didswap) 98524a1cce3SDavid Greenman wakeup(&proc0); 986df8bae1dSRodney W. Grimes } 987df8bae1dSRodney W. Grimes 988f708ef1bSPoul-Henning Kamp static void 989b61ce5b0SJeff Roberson swapclear(p) 990b61ce5b0SJeff Roberson struct proc *p; 991b61ce5b0SJeff Roberson { 992b61ce5b0SJeff Roberson struct thread *td; 993b61ce5b0SJeff Roberson 994b61ce5b0SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 995b61ce5b0SJeff Roberson 996b61ce5b0SJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 997b61ce5b0SJeff Roberson thread_lock(td); 998b61ce5b0SJeff Roberson td->td_flags |= TDF_INMEM; 999b61ce5b0SJeff Roberson td->td_flags &= ~TDF_SWAPINREQ; 1000b61ce5b0SJeff Roberson TD_CLR_SWAPPED(td); 1001b61ce5b0SJeff Roberson if (TD_CAN_RUN(td)) 1002da7bbd2cSJohn Baldwin if (setrunnable(td)) { 1003da7bbd2cSJohn Baldwin #ifdef INVARIANTS 1004da7bbd2cSJohn Baldwin /* 1005da7bbd2cSJohn Baldwin * XXX: We just cleared TDI_SWAPPED 1006da7bbd2cSJohn Baldwin * above and set TDF_INMEM, so this 1007da7bbd2cSJohn Baldwin * should never happen. 1008da7bbd2cSJohn Baldwin */ 1009da7bbd2cSJohn Baldwin panic("not waking up swapper"); 1010da7bbd2cSJohn Baldwin #endif 1011da7bbd2cSJohn Baldwin } 1012b61ce5b0SJeff Roberson thread_unlock(td); 1013b61ce5b0SJeff Roberson } 1014b61ce5b0SJeff Roberson p->p_flag &= ~(P_SWAPPINGIN|P_SWAPPINGOUT); 1015b61ce5b0SJeff Roberson p->p_flag |= P_INMEM; 1016b61ce5b0SJeff Roberson } 1017b61ce5b0SJeff Roberson 1018b61ce5b0SJeff Roberson static int 1019df8bae1dSRodney W. Grimes swapout(p) 102054d92145SMatthew Dillon struct proc *p; 1021df8bae1dSRodney W. Grimes { 1022b40ce416SJulian Elischer struct thread *td; 1023df8bae1dSRodney W. Grimes 1024ea754954SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1025d3a34985SJohn Dyson #if defined(SWAP_DEBUG) 1026d3a34985SJohn Dyson printf("swapping out %d\n", p->p_pid); 1027d3a34985SJohn Dyson #endif 10281d7b9ed2SJulian Elischer 10291d7b9ed2SJulian Elischer /* 10309eb881f8SSeigo Tanimura * The states of this process and its threads may have changed 10319eb881f8SSeigo Tanimura * by now. Assuming that there is only one pageout daemon thread, 10329eb881f8SSeigo Tanimura * this process should still be in memory. 10339eb881f8SSeigo Tanimura */ 1034b61ce5b0SJeff Roberson KASSERT((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) == P_INMEM, 10359eb881f8SSeigo Tanimura ("swapout: lost a swapout race?")); 10369eb881f8SSeigo Tanimura 1037df8bae1dSRodney W. Grimes /* 103826f9a767SRodney W. Grimes * remember the process resident count 1039df8bae1dSRodney W. Grimes */ 1040b1028ad1SLuoqi Chen p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 1041b61ce5b0SJeff Roberson /* 1042b61ce5b0SJeff Roberson * Check and mark all threads before we proceed. 1043b61ce5b0SJeff Roberson */ 1044b61ce5b0SJeff Roberson p->p_flag &= ~P_INMEM; 1045b61ce5b0SJeff Roberson p->p_flag |= P_SWAPPINGOUT; 1046982d11f8SJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 1047982d11f8SJeff Roberson thread_lock(td); 1048b61ce5b0SJeff Roberson if (!thread_safetoswapout(td)) { 1049b61ce5b0SJeff Roberson thread_unlock(td); 1050b61ce5b0SJeff Roberson swapclear(p); 1051b61ce5b0SJeff Roberson return (EBUSY); 1052b61ce5b0SJeff Roberson } 1053b61ce5b0SJeff Roberson td->td_flags &= ~TDF_INMEM; 1054664f718bSJohn Baldwin TD_SET_SWAPPED(td); 1055982d11f8SJeff Roberson thread_unlock(td); 1056982d11f8SJeff Roberson } 1057b61ce5b0SJeff Roberson td = FIRST_THREAD_IN_PROC(p); 1058b61ce5b0SJeff Roberson ++td->td_ru.ru_nswap; 1059b61ce5b0SJeff Roberson PROC_UNLOCK(p); 106026f9a767SRodney W. Grimes 1061b61ce5b0SJeff Roberson /* 1062b61ce5b0SJeff Roberson * This list is stable because all threads are now prevented from 1063b61ce5b0SJeff Roberson * running. The list is only modified in the context of a running 1064b61ce5b0SJeff Roberson * thread in this process. 1065b61ce5b0SJeff Roberson */ 1066664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) 106749a2507bSAlan Cox vm_thread_swapout(td); 1068664f718bSJohn Baldwin 1069664f718bSJohn Baldwin PROC_LOCK(p); 1070b61ce5b0SJeff Roberson p->p_flag &= ~P_SWAPPINGOUT; 1071258853abSJeff Roberson p->p_swtick = ticks; 1072b61ce5b0SJeff Roberson return (0); 1073df8bae1dSRodney W. Grimes } 10745afce282SDavid Greenman #endif /* !NO_SWAPPING */ 1075