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 3121a4fcaebSMarcel Moolenaar void 3131a4fcaebSMarcel Moolenaar vm_sync_icache(vm_map_t map, vm_offset_t va, vm_offset_t sz) 3141a4fcaebSMarcel Moolenaar { 3151a4fcaebSMarcel Moolenaar 3161a4fcaebSMarcel Moolenaar pmap_sync_icache(map->pmap, va, sz); 3171a4fcaebSMarcel Moolenaar } 3181a4fcaebSMarcel Moolenaar 3198a945d10SKonstantin Belousov struct kstack_cache_entry { 3208a945d10SKonstantin Belousov vm_object_t ksobj; 3218a945d10SKonstantin Belousov struct kstack_cache_entry *next_ks_entry; 3228a945d10SKonstantin Belousov }; 3238a945d10SKonstantin Belousov 3248a945d10SKonstantin Belousov static struct kstack_cache_entry *kstack_cache; 3258a945d10SKonstantin Belousov static int kstack_cache_size = 128; 3268a945d10SKonstantin Belousov static int kstacks; 3278a945d10SKonstantin Belousov static struct mtx kstack_cache_mtx; 3288a945d10SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, kstack_cache_size, CTLFLAG_RW, &kstack_cache_size, 0, 3298a945d10SKonstantin Belousov ""); 3308a945d10SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, kstacks, CTLFLAG_RD, &kstacks, 0, 3318a945d10SKonstantin Belousov ""); 3328a945d10SKonstantin Belousov 33349a2507bSAlan Cox #ifndef KSTACK_MAX_PAGES 33449a2507bSAlan Cox #define KSTACK_MAX_PAGES 32 33549a2507bSAlan Cox #endif 33649a2507bSAlan Cox 33749a2507bSAlan Cox /* 33849a2507bSAlan Cox * Create the kernel stack (including pcb for i386) for a new thread. 33949a2507bSAlan Cox * This routine directly affects the fork perf for a process and 34049a2507bSAlan Cox * create performance for a thread. 34149a2507bSAlan Cox */ 34289b57fcfSKonstantin Belousov int 34349a2507bSAlan Cox vm_thread_new(struct thread *td, int pages) 34449a2507bSAlan Cox { 34549a2507bSAlan Cox vm_object_t ksobj; 34649a2507bSAlan Cox vm_offset_t ks; 34749a2507bSAlan Cox vm_page_t m, ma[KSTACK_MAX_PAGES]; 3488a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce; 34949a2507bSAlan Cox int i; 35049a2507bSAlan Cox 35149a2507bSAlan Cox /* Bounds check */ 35249a2507bSAlan Cox if (pages <= 1) 35349a2507bSAlan Cox pages = KSTACK_PAGES; 35449a2507bSAlan Cox else if (pages > KSTACK_MAX_PAGES) 35549a2507bSAlan Cox pages = KSTACK_MAX_PAGES; 3568a945d10SKonstantin Belousov 3578a945d10SKonstantin Belousov if (pages == KSTACK_PAGES) { 3588a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 3598a945d10SKonstantin Belousov if (kstack_cache != NULL) { 3608a945d10SKonstantin Belousov ks_ce = kstack_cache; 3618a945d10SKonstantin Belousov kstack_cache = ks_ce->next_ks_entry; 3628a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 3638a945d10SKonstantin Belousov 3648a945d10SKonstantin Belousov td->td_kstack_obj = ks_ce->ksobj; 3658a945d10SKonstantin Belousov td->td_kstack = (vm_offset_t)ks_ce; 3668a945d10SKonstantin Belousov td->td_kstack_pages = KSTACK_PAGES; 3678a945d10SKonstantin Belousov return (1); 3688a945d10SKonstantin Belousov } 3698a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 3708a945d10SKonstantin Belousov } 3718a945d10SKonstantin Belousov 37249a2507bSAlan Cox /* 37349a2507bSAlan Cox * Allocate an object for the kstack. 37449a2507bSAlan Cox */ 37549a2507bSAlan Cox ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 376374ae2a3SJeff Roberson 37749a2507bSAlan Cox /* 37849a2507bSAlan Cox * Get a kernel virtual address for this thread's kstack. 37949a2507bSAlan Cox */ 38049a2507bSAlan Cox ks = kmem_alloc_nofault(kernel_map, 38149a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 38289b57fcfSKonstantin Belousov if (ks == 0) { 38389b57fcfSKonstantin Belousov printf("vm_thread_new: kstack allocation failed\n"); 38489b57fcfSKonstantin Belousov vm_object_deallocate(ksobj); 38589b57fcfSKonstantin Belousov return (0); 38689b57fcfSKonstantin Belousov } 38789b57fcfSKonstantin Belousov 3888a945d10SKonstantin Belousov atomic_add_int(&kstacks, 1); 38949a2507bSAlan Cox if (KSTACK_GUARD_PAGES != 0) { 39049a2507bSAlan Cox pmap_qremove(ks, KSTACK_GUARD_PAGES); 39149a2507bSAlan Cox ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 39249a2507bSAlan Cox } 39389b57fcfSKonstantin Belousov td->td_kstack_obj = ksobj; 39449a2507bSAlan Cox td->td_kstack = ks; 39549a2507bSAlan Cox /* 39649a2507bSAlan Cox * Knowing the number of pages allocated is useful when you 39749a2507bSAlan Cox * want to deallocate them. 39849a2507bSAlan Cox */ 39949a2507bSAlan Cox td->td_kstack_pages = pages; 40049a2507bSAlan Cox /* 40149a2507bSAlan Cox * For the length of the stack, link in a real page of ram for each 40249a2507bSAlan Cox * page of stack. 40349a2507bSAlan Cox */ 40449a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 40549a2507bSAlan Cox for (i = 0; i < pages; i++) { 40649a2507bSAlan Cox /* 40749a2507bSAlan Cox * Get a kernel stack page. 40849a2507bSAlan Cox */ 409ddf4bb37SAlan Cox m = vm_page_grab(ksobj, i, VM_ALLOC_NOBUSY | 41049a2507bSAlan Cox VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED); 41149a2507bSAlan Cox ma[i] = m; 41249a2507bSAlan Cox m->valid = VM_PAGE_BITS_ALL; 41349a2507bSAlan Cox } 41449a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 41549a2507bSAlan Cox pmap_qenter(ks, ma, pages); 41689b57fcfSKonstantin Belousov return (1); 41749a2507bSAlan Cox } 41849a2507bSAlan Cox 4198a945d10SKonstantin Belousov static void 4208a945d10SKonstantin Belousov vm_thread_stack_dispose(vm_object_t ksobj, vm_offset_t ks, int pages) 42149a2507bSAlan Cox { 42249a2507bSAlan Cox vm_page_t m; 4238a945d10SKonstantin Belousov int i; 42449a2507bSAlan Cox 4258a945d10SKonstantin Belousov atomic_add_int(&kstacks, -1); 42649a2507bSAlan Cox pmap_qremove(ks, pages); 42749a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 42849a2507bSAlan Cox for (i = 0; i < pages; i++) { 42949a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 43049a2507bSAlan Cox if (m == NULL) 43149a2507bSAlan Cox panic("vm_thread_dispose: kstack already missing?"); 43249a2507bSAlan Cox vm_page_lock_queues(); 43349a2507bSAlan Cox vm_page_unwire(m, 0); 43449a2507bSAlan Cox vm_page_free(m); 43549a2507bSAlan Cox vm_page_unlock_queues(); 43649a2507bSAlan Cox } 43749a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 43849a2507bSAlan Cox vm_object_deallocate(ksobj); 43949a2507bSAlan Cox kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 44049a2507bSAlan Cox (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 441c3cf0b47SKonstantin Belousov } 442c3cf0b47SKonstantin Belousov 443c3cf0b47SKonstantin Belousov /* 4448a945d10SKonstantin Belousov * Dispose of a thread's kernel stack. 4458a945d10SKonstantin Belousov */ 4468a945d10SKonstantin Belousov void 4478a945d10SKonstantin Belousov vm_thread_dispose(struct thread *td) 4488a945d10SKonstantin Belousov { 4498a945d10SKonstantin Belousov vm_object_t ksobj; 4508a945d10SKonstantin Belousov vm_offset_t ks; 4518a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce; 4528a945d10SKonstantin Belousov int pages; 4538a945d10SKonstantin Belousov 4548a945d10SKonstantin Belousov pages = td->td_kstack_pages; 4558a945d10SKonstantin Belousov ksobj = td->td_kstack_obj; 4568a945d10SKonstantin Belousov ks = td->td_kstack; 4578a945d10SKonstantin Belousov td->td_kstack = 0; 4588a945d10SKonstantin Belousov td->td_kstack_pages = 0; 4598a945d10SKonstantin Belousov if (pages == KSTACK_PAGES && kstacks <= kstack_cache_size) { 4608a945d10SKonstantin Belousov ks_ce = (struct kstack_cache_entry *)ks; 4618a945d10SKonstantin Belousov ks_ce->ksobj = ksobj; 4628a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 4638a945d10SKonstantin Belousov ks_ce->next_ks_entry = kstack_cache; 4648a945d10SKonstantin Belousov kstack_cache = ks_ce; 4658a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 4668a945d10SKonstantin Belousov return; 4678a945d10SKonstantin Belousov } 4688a945d10SKonstantin Belousov vm_thread_stack_dispose(ksobj, ks, pages); 4698a945d10SKonstantin Belousov } 4708a945d10SKonstantin Belousov 4718a945d10SKonstantin Belousov static void 4728a945d10SKonstantin Belousov vm_thread_stack_lowmem(void *nulll) 4738a945d10SKonstantin Belousov { 4748a945d10SKonstantin Belousov struct kstack_cache_entry *ks_ce, *ks_ce1; 4758a945d10SKonstantin Belousov 4768a945d10SKonstantin Belousov mtx_lock(&kstack_cache_mtx); 4778a945d10SKonstantin Belousov ks_ce = kstack_cache; 4788a945d10SKonstantin Belousov kstack_cache = NULL; 4798a945d10SKonstantin Belousov mtx_unlock(&kstack_cache_mtx); 4808a945d10SKonstantin Belousov 4818a945d10SKonstantin Belousov while (ks_ce != NULL) { 4828a945d10SKonstantin Belousov ks_ce1 = ks_ce; 4838a945d10SKonstantin Belousov ks_ce = ks_ce->next_ks_entry; 4848a945d10SKonstantin Belousov 4858a945d10SKonstantin Belousov vm_thread_stack_dispose(ks_ce1->ksobj, (vm_offset_t)ks_ce1, 4868a945d10SKonstantin Belousov KSTACK_PAGES); 4878a945d10SKonstantin Belousov } 4888a945d10SKonstantin Belousov } 4898a945d10SKonstantin Belousov 4908a945d10SKonstantin Belousov static void 4918a945d10SKonstantin Belousov kstack_cache_init(void *nulll) 4928a945d10SKonstantin Belousov { 4938a945d10SKonstantin Belousov 4948a945d10SKonstantin Belousov EVENTHANDLER_REGISTER(vm_lowmem, vm_thread_stack_lowmem, NULL, 4958a945d10SKonstantin Belousov EVENTHANDLER_PRI_ANY); 4968a945d10SKonstantin Belousov } 4978a945d10SKonstantin Belousov 4988a945d10SKonstantin Belousov MTX_SYSINIT(kstack_cache, &kstack_cache_mtx, "kstkch", MTX_DEF); 4998a945d10SKonstantin Belousov SYSINIT(vm_kstacks, SI_SUB_KTHREAD_INIT, SI_ORDER_ANY, kstack_cache_init, NULL); 5008a945d10SKonstantin Belousov 5018a945d10SKonstantin Belousov /* 50249a2507bSAlan Cox * Allow a thread's kernel stack to be paged out. 50349a2507bSAlan Cox */ 50449a2507bSAlan Cox void 50549a2507bSAlan Cox vm_thread_swapout(struct thread *td) 50649a2507bSAlan Cox { 50749a2507bSAlan Cox vm_object_t ksobj; 50849a2507bSAlan Cox vm_page_t m; 50949a2507bSAlan Cox int i, pages; 51049a2507bSAlan Cox 511710338e9SMarcel Moolenaar cpu_thread_swapout(td); 51249a2507bSAlan Cox pages = td->td_kstack_pages; 51349a2507bSAlan Cox ksobj = td->td_kstack_obj; 51449a2507bSAlan Cox pmap_qremove(td->td_kstack, pages); 51549a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 51649a2507bSAlan Cox for (i = 0; i < pages; i++) { 51749a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 51849a2507bSAlan Cox if (m == NULL) 51949a2507bSAlan Cox panic("vm_thread_swapout: kstack already missing?"); 52049a2507bSAlan Cox vm_page_lock_queues(); 52149a2507bSAlan Cox vm_page_dirty(m); 52249a2507bSAlan Cox vm_page_unwire(m, 0); 52349a2507bSAlan Cox vm_page_unlock_queues(); 52449a2507bSAlan Cox } 52549a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 52649a2507bSAlan Cox } 52749a2507bSAlan Cox 52849a2507bSAlan Cox /* 52949a2507bSAlan Cox * Bring the kernel stack for a specified thread back in. 53049a2507bSAlan Cox */ 53149a2507bSAlan Cox void 53249a2507bSAlan Cox vm_thread_swapin(struct thread *td) 53349a2507bSAlan Cox { 53449a2507bSAlan Cox vm_object_t ksobj; 53549a2507bSAlan Cox vm_page_t m, ma[KSTACK_MAX_PAGES]; 53649a2507bSAlan Cox int i, pages, rv; 53749a2507bSAlan Cox 53849a2507bSAlan Cox pages = td->td_kstack_pages; 53949a2507bSAlan Cox ksobj = td->td_kstack_obj; 54049a2507bSAlan Cox VM_OBJECT_LOCK(ksobj); 54149a2507bSAlan Cox for (i = 0; i < pages; i++) { 54249a2507bSAlan Cox m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 54349a2507bSAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 54449a2507bSAlan Cox rv = vm_pager_get_pages(ksobj, &m, 1, 0); 54549a2507bSAlan Cox if (rv != VM_PAGER_OK) 54649a2507bSAlan Cox panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid); 54749a2507bSAlan Cox m = vm_page_lookup(ksobj, i); 54849a2507bSAlan Cox } 54949a2507bSAlan Cox ma[i] = m; 55049a2507bSAlan Cox vm_page_lock_queues(); 55149a2507bSAlan Cox vm_page_wire(m); 55249a2507bSAlan Cox vm_page_unlock_queues(); 55366bdd5d6SAlan Cox vm_page_wakeup(m); 55449a2507bSAlan Cox } 55549a2507bSAlan Cox VM_OBJECT_UNLOCK(ksobj); 55649a2507bSAlan Cox pmap_qenter(td->td_kstack, ma, pages); 557710338e9SMarcel Moolenaar cpu_thread_swapin(td); 55849a2507bSAlan Cox } 55949a2507bSAlan Cox 560a136efe9SPeter Wemm /* 561df8bae1dSRodney W. Grimes * Implement fork's actions on an address space. 562df8bae1dSRodney W. Grimes * Here we arrange for the address space to be copied or referenced, 563df8bae1dSRodney W. Grimes * allocate a user struct (pcb and kernel stack), then call the 564df8bae1dSRodney W. Grimes * machine-dependent layer to fill those in and make the new process 565a2a1c95cSPeter Wemm * ready to run. The new process is set up so that it returns directly 566a2a1c95cSPeter Wemm * to user mode to avoid stack copying and relocation problems. 567df8bae1dSRodney W. Grimes */ 56889b57fcfSKonstantin Belousov int 56989b57fcfSKonstantin Belousov vm_forkproc(td, p2, td2, vm2, flags) 570b40ce416SJulian Elischer struct thread *td; 571b40ce416SJulian Elischer struct proc *p2; 572079b7badSJulian Elischer struct thread *td2; 57389b57fcfSKonstantin Belousov struct vmspace *vm2; 574a2a1c95cSPeter Wemm int flags; 575df8bae1dSRodney W. Grimes { 576b40ce416SJulian Elischer struct proc *p1 = td->td_proc; 57789b57fcfSKonstantin Belousov int error; 578df8bae1dSRodney W. Grimes 57991c28bfdSLuoqi Chen if ((flags & RFPROC) == 0) { 58091c28bfdSLuoqi Chen /* 58191c28bfdSLuoqi Chen * Divorce the memory, if it is shared, essentially 58291c28bfdSLuoqi Chen * this changes shared memory amongst threads, into 58391c28bfdSLuoqi Chen * COW locally. 58491c28bfdSLuoqi Chen */ 58591c28bfdSLuoqi Chen if ((flags & RFMEM) == 0) { 58691c28bfdSLuoqi Chen if (p1->p_vmspace->vm_refcnt > 1) { 58789b57fcfSKonstantin Belousov error = vmspace_unshare(p1); 58889b57fcfSKonstantin Belousov if (error) 58989b57fcfSKonstantin Belousov return (error); 59091c28bfdSLuoqi Chen } 59191c28bfdSLuoqi Chen } 592079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 59389b57fcfSKonstantin Belousov return (0); 59491c28bfdSLuoqi Chen } 59591c28bfdSLuoqi Chen 5965856e12eSJohn Dyson if (flags & RFMEM) { 5975856e12eSJohn Dyson p2->p_vmspace = p1->p_vmspace; 5981a276a3fSAlan Cox atomic_add_int(&p1->p_vmspace->vm_refcnt, 1); 5995856e12eSJohn Dyson } 6005856e12eSJohn Dyson 60190ecac61SMatthew Dillon while (vm_page_count_severe()) { 60226f9a767SRodney W. Grimes VM_WAIT; 6030d94caffSDavid Greenman } 60426f9a767SRodney W. Grimes 6055856e12eSJohn Dyson if ((flags & RFMEM) == 0) { 60689b57fcfSKonstantin Belousov p2->p_vmspace = vm2; 607df8bae1dSRodney W. Grimes if (p1->p_vmspace->vm_shm) 608dabee6feSPeter Wemm shmfork(p1, p2); 609a2a1c95cSPeter Wemm } 610df8bae1dSRodney W. Grimes 61139fb8e6bSJulian Elischer /* 612a2a1c95cSPeter Wemm * cpu_fork will copy and update the pcb, set up the kernel stack, 613a2a1c95cSPeter Wemm * and make the child ready to run. 614df8bae1dSRodney W. Grimes */ 615079b7badSJulian Elischer cpu_fork(td, p2, td2, flags); 61689b57fcfSKonstantin Belousov return (0); 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes /* 620eb30c1c0SPeter Wemm * Called after process has been wait(2)'ed apon and is being reaped. 621eb30c1c0SPeter Wemm * The idea is to reclaim resources that we could not reclaim while 622eb30c1c0SPeter Wemm * the process was still executing. 623eb30c1c0SPeter Wemm */ 624eb30c1c0SPeter Wemm void 625eb30c1c0SPeter Wemm vm_waitproc(p) 626eb30c1c0SPeter Wemm struct proc *p; 627eb30c1c0SPeter Wemm { 628eb30c1c0SPeter Wemm 629582ec34cSAlfred Perlstein vmspace_exitfree(p); /* and clean-out the vmspace */ 630eb30c1c0SPeter Wemm } 631eb30c1c0SPeter Wemm 632eb30c1c0SPeter Wemm /* 633df8bae1dSRodney W. Grimes * Set default limits for VM system. 634df8bae1dSRodney W. Grimes * Called for proc 0, and then inherited by all others. 6352b14f991SJulian Elischer * 6362b14f991SJulian Elischer * XXX should probably act directly on proc0. 637df8bae1dSRodney W. Grimes */ 6382b14f991SJulian Elischer static void 6392b14f991SJulian Elischer vm_init_limits(udata) 6404590fd3aSDavid Greenman void *udata; 641df8bae1dSRodney W. Grimes { 64254d92145SMatthew Dillon struct proc *p = udata; 64391d5354aSJohn Baldwin struct plimit *limp; 644bbc0ec52SDavid Greenman int rss_limit; 645df8bae1dSRodney W. Grimes 646df8bae1dSRodney W. Grimes /* 6470d94caffSDavid Greenman * Set up the initial limits on process VM. Set the maximum resident 6480d94caffSDavid Greenman * set size to be half of (reasonably) available memory. Since this 6490d94caffSDavid Greenman * is a soft limit, it comes into effect only when the system is out 6500d94caffSDavid Greenman * of memory - half of main memory helps to favor smaller processes, 651bbc0ec52SDavid Greenman * and reduces thrashing of the object cache. 652df8bae1dSRodney W. Grimes */ 65391d5354aSJohn Baldwin limp = p->p_limit; 65491d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 65591d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 65691d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 65791d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 658dd0bd066SDavid Greenman /* limit the limit to no less than 2MB */ 6592feb50bfSAttilio Rao rss_limit = max(cnt.v_free_count, 512); 66091d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 66191d5354aSJohn Baldwin limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 662df8bae1dSRodney W. Grimes } 663df8bae1dSRodney W. Grimes 66426f9a767SRodney W. Grimes void 66526f9a767SRodney W. Grimes faultin(p) 66626f9a767SRodney W. Grimes struct proc *p; 66726f9a767SRodney W. Grimes { 66811edc1e0SJohn Baldwin #ifdef NO_SWAPPING 66911edc1e0SJohn Baldwin 67011edc1e0SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 671b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 67211edc1e0SJohn Baldwin panic("faultin: proc swapped out with NO_SWAPPING!"); 67311edc1e0SJohn Baldwin #else /* !NO_SWAPPING */ 674664f718bSJohn Baldwin struct thread *td; 67526f9a767SRodney W. Grimes 676c96d52a9SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 6771d7b9ed2SJulian Elischer /* 6781d7b9ed2SJulian Elischer * If another process is swapping in this process, 6791d7b9ed2SJulian Elischer * just wait until it finishes. 6801d7b9ed2SJulian Elischer */ 681b61ce5b0SJeff Roberson if (p->p_flag & P_SWAPPINGIN) { 682b61ce5b0SJeff Roberson while (p->p_flag & P_SWAPPINGIN) 683b61ce5b0SJeff Roberson msleep(&p->p_flag, &p->p_mtx, PVM, "faultin", 0); 684b61ce5b0SJeff Roberson return; 685b61ce5b0SJeff Roberson } 686b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) { 687664f718bSJohn Baldwin /* 688664f718bSJohn Baldwin * Don't let another thread swap process p out while we are 689664f718bSJohn Baldwin * busy swapping it in. 690664f718bSJohn Baldwin */ 691664f718bSJohn Baldwin ++p->p_lock; 692b61ce5b0SJeff Roberson p->p_flag |= P_SWAPPINGIN; 69345ece682SJohn Baldwin PROC_UNLOCK(p); 69426f9a767SRodney W. Grimes 695b61ce5b0SJeff Roberson /* 696b61ce5b0SJeff Roberson * We hold no lock here because the list of threads 697b61ce5b0SJeff Roberson * can not change while all threads in the process are 698b61ce5b0SJeff Roberson * swapped out. 699b61ce5b0SJeff Roberson */ 700664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) 70149a2507bSAlan Cox vm_thread_swapin(td); 70245ece682SJohn Baldwin PROC_LOCK(p); 703b61ce5b0SJeff Roberson swapclear(p); 704258853abSJeff Roberson p->p_swtick = ticks; 70526f9a767SRodney W. Grimes 706b61ce5b0SJeff Roberson wakeup(&p->p_flag); 70726f9a767SRodney W. Grimes 708664f718bSJohn Baldwin /* Allow other threads to swap p out now. */ 70926f9a767SRodney W. Grimes --p->p_lock; 71026f9a767SRodney W. Grimes } 71111edc1e0SJohn Baldwin #endif /* NO_SWAPPING */ 71226f9a767SRodney W. Grimes } 71326f9a767SRodney W. Grimes 714df8bae1dSRodney W. Grimes /* 71526f9a767SRodney W. Grimes * This swapin algorithm attempts to swap-in processes only if there 71626f9a767SRodney W. Grimes * is enough space for them. Of course, if a process waits for a long 71726f9a767SRodney W. Grimes * time, it will be swapped in anyway. 7180384fff8SJason Evans * 71910c447faSAlan Cox * Giant is held on entry. 720df8bae1dSRodney W. Grimes */ 7212b14f991SJulian Elischer /* ARGSUSED*/ 7222b14f991SJulian Elischer static void 723d841aaa7SBruce Evans scheduler(dummy) 724d841aaa7SBruce Evans void *dummy; 725df8bae1dSRodney W. Grimes { 72654d92145SMatthew Dillon struct proc *p; 727e602ba25SJulian Elischer struct thread *td; 728df8bae1dSRodney W. Grimes struct proc *pp; 729258853abSJeff Roberson int slptime; 730258853abSJeff Roberson int swtime; 731df8bae1dSRodney W. Grimes int ppri; 732258853abSJeff Roberson int pri; 733df8bae1dSRodney W. Grimes 734c96d52a9SJohn Baldwin mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 73510c447faSAlan Cox mtx_unlock(&Giant); 7360384fff8SJason Evans 737df8bae1dSRodney W. Grimes loop: 73890ecac61SMatthew Dillon if (vm_page_count_min()) { 7390d94caffSDavid Greenman VM_WAIT; 74090ecac61SMatthew Dillon goto loop; 7410d94caffSDavid Greenman } 74226f9a767SRodney W. Grimes 743df8bae1dSRodney W. Grimes pp = NULL; 744df8bae1dSRodney W. Grimes ppri = INT_MIN; 7451005a129SJohn Baldwin sx_slock(&allproc_lock); 746b40ce416SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 747b61ce5b0SJeff Roberson PROC_LOCK(p); 748b61ce5b0SJeff Roberson if (p->p_flag & (P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) { 749b61ce5b0SJeff Roberson PROC_UNLOCK(p); 750e602ba25SJulian Elischer continue; 751e602ba25SJulian Elischer } 752258853abSJeff Roberson swtime = (ticks - p->p_swtick) / hz; 753e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 7541d7b9ed2SJulian Elischer /* 75571fad9fdSJulian Elischer * An otherwise runnable thread of a process 75671fad9fdSJulian Elischer * swapped out has only the TDI_SWAPPED bit set. 75771fad9fdSJulian Elischer * 7581d7b9ed2SJulian Elischer */ 759982d11f8SJeff Roberson thread_lock(td); 76071fad9fdSJulian Elischer if (td->td_inhibitors == TDI_SWAPPED) { 761258853abSJeff Roberson slptime = (ticks - td->td_slptick) / hz; 762258853abSJeff Roberson pri = swtime + slptime; 763b61ce5b0SJeff Roberson if ((td->td_flags & TDF_SWAPINREQ) == 0) 764fa885116SJulian Elischer pri -= p->p_nice * 8; 76526f9a767SRodney W. Grimes /* 766ad1e7d28SJulian Elischer * if this thread is higher priority 767b40ce416SJulian Elischer * and there is enough space, then select 768b40ce416SJulian Elischer * this process instead of the previous 769b40ce416SJulian Elischer * selection. 77026f9a767SRodney W. Grimes */ 7710d94caffSDavid Greenman if (pri > ppri) { 772df8bae1dSRodney W. Grimes pp = p; 773df8bae1dSRodney W. Grimes ppri = pri; 774df8bae1dSRodney W. Grimes } 775df8bae1dSRodney W. Grimes } 776982d11f8SJeff Roberson thread_unlock(td); 777b40ce416SJulian Elischer } 778b61ce5b0SJeff Roberson PROC_UNLOCK(p); 779df8bae1dSRodney W. Grimes } 7801005a129SJohn Baldwin sx_sunlock(&allproc_lock); 78126f9a767SRodney W. Grimes 782df8bae1dSRodney W. Grimes /* 783a669a6e9SJohn Dyson * Nothing to do, back to sleep. 784df8bae1dSRodney W. Grimes */ 785df8bae1dSRodney W. Grimes if ((p = pp) == NULL) { 786da7bbd2cSJohn Baldwin tsleep(&proc0, PVM, "sched", maxslp * hz / 2); 787df8bae1dSRodney W. Grimes goto loop; 788df8bae1dSRodney W. Grimes } 7891d7b9ed2SJulian Elischer PROC_LOCK(p); 7901d7b9ed2SJulian Elischer 7911d7b9ed2SJulian Elischer /* 7921d7b9ed2SJulian Elischer * Another process may be bringing or may have already 7931d7b9ed2SJulian Elischer * brought this process in while we traverse all threads. 7941d7b9ed2SJulian Elischer * Or, this process may even be being swapped out again. 7951d7b9ed2SJulian Elischer */ 796b61ce5b0SJeff Roberson if (p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) { 7971d7b9ed2SJulian Elischer PROC_UNLOCK(p); 7981d7b9ed2SJulian Elischer goto loop; 7991d7b9ed2SJulian Elischer } 8001d7b9ed2SJulian Elischer 801df8bae1dSRodney W. Grimes /* 80226f9a767SRodney W. Grimes * We would like to bring someone in. (only if there is space). 803e602ba25SJulian Elischer * [What checks the space? ] 804df8bae1dSRodney W. Grimes */ 80526f9a767SRodney W. Grimes faultin(p); 80645ece682SJohn Baldwin PROC_UNLOCK(p); 807df8bae1dSRodney W. Grimes goto loop; 808df8bae1dSRodney W. Grimes } 809df8bae1dSRodney W. Grimes 810da7bbd2cSJohn Baldwin void 811da7bbd2cSJohn Baldwin kick_proc0(void) 812d13ec713SStephan Uphoff { 813d13ec713SStephan Uphoff 814da7bbd2cSJohn Baldwin wakeup(&proc0); 815d13ec713SStephan Uphoff } 816d13ec713SStephan Uphoff 8175afce282SDavid Greenman #ifndef NO_SWAPPING 8185afce282SDavid Greenman 819ceb0cf87SJohn Dyson /* 820ceb0cf87SJohn Dyson * Swap_idle_threshold1 is the guaranteed swapped in time for a process 821ceb0cf87SJohn Dyson */ 822303b270bSEivind Eklund static int swap_idle_threshold1 = 2; 8232a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW, 8249faaf3b3STom Rhodes &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process"); 825ceb0cf87SJohn Dyson 826ceb0cf87SJohn Dyson /* 827ceb0cf87SJohn Dyson * Swap_idle_threshold2 is the time that a process can be idle before 828ceb0cf87SJohn Dyson * it will be swapped out, if idle swapping is enabled. 829ceb0cf87SJohn Dyson */ 830303b270bSEivind Eklund static int swap_idle_threshold2 = 10; 8312a3eeaa2STom Rhodes SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW, 8329faaf3b3STom Rhodes &swap_idle_threshold2, 0, "Time before a process will be swapped out"); 833ceb0cf87SJohn Dyson 834df8bae1dSRodney W. Grimes /* 835df8bae1dSRodney W. Grimes * Swapout is driven by the pageout daemon. Very simple, we find eligible 836b61ce5b0SJeff Roberson * procs and swap out their stacks. We try to always "swap" at least one 837df8bae1dSRodney W. Grimes * process in case we need the room for a swapin. 838df8bae1dSRodney W. Grimes * If any procs have been sleeping/stopped for at least maxslp seconds, 839df8bae1dSRodney W. Grimes * they are swapped. Else, we swap the longest-sleeping or stopped process, 840df8bae1dSRodney W. Grimes * if any, otherwise the longest-resident process. 841df8bae1dSRodney W. Grimes */ 842df8bae1dSRodney W. Grimes void 8433a2dc656SJohn Dyson swapout_procs(action) 8443a2dc656SJohn Dyson int action; 845df8bae1dSRodney W. Grimes { 84654d92145SMatthew Dillon struct proc *p; 847e602ba25SJulian Elischer struct thread *td; 848df8bae1dSRodney W. Grimes int didswap = 0; 849df8bae1dSRodney W. Grimes 8500d94caffSDavid Greenman retry: 8513a2189d4SJohn Baldwin sx_slock(&allproc_lock); 852e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 853b18bfc3dSJohn Dyson struct vmspace *vm; 854b40ce416SJulian Elischer int minslptime = 100000; 855258853abSJeff Roberson int slptime; 856b18bfc3dSJohn Dyson 8579eb881f8SSeigo Tanimura /* 858b1f99ebeSSeigo Tanimura * Watch out for a process in 859b1f99ebeSSeigo Tanimura * creation. It may have no 8601c865ac7SJohn Baldwin * address space or lock yet. 8611c865ac7SJohn Baldwin */ 862b61ce5b0SJeff Roberson if (p->p_state == PRS_NEW) 8631c865ac7SJohn Baldwin continue; 8641c865ac7SJohn Baldwin /* 865b1f99ebeSSeigo Tanimura * An aio daemon switches its 866b1f99ebeSSeigo Tanimura * address space while running. 867b1f99ebeSSeigo Tanimura * Perform a quick check whether 868b1f99ebeSSeigo Tanimura * a process has P_SYSTEM. 8699eb881f8SSeigo Tanimura */ 8708f887403SJohn Baldwin if ((p->p_flag & P_SYSTEM) != 0) 871b1f99ebeSSeigo Tanimura continue; 8721c865ac7SJohn Baldwin /* 8731c865ac7SJohn Baldwin * Do not swapout a process that 8741c865ac7SJohn Baldwin * is waiting for VM data 8751c865ac7SJohn Baldwin * structures as there is a possible 8761c865ac7SJohn Baldwin * deadlock. Test this first as 8771c865ac7SJohn Baldwin * this may block. 8781c865ac7SJohn Baldwin * 8791c865ac7SJohn Baldwin * Lock the map until swapout 8801c865ac7SJohn Baldwin * finishes, or a thread of this 8811c865ac7SJohn Baldwin * process may attempt to alter 8821c865ac7SJohn Baldwin * the map. 8831c865ac7SJohn Baldwin */ 88457051fdcSTor Egge vm = vmspace_acquire_ref(p); 88557051fdcSTor Egge if (vm == NULL) 88657051fdcSTor Egge continue; 8879eb881f8SSeigo Tanimura if (!vm_map_trylock(&vm->vm_map)) 8889eb881f8SSeigo Tanimura goto nextproc1; 8899eb881f8SSeigo Tanimura 8905074aecdSJohn Baldwin PROC_LOCK(p); 89169b40456SJohn Baldwin if (p->p_lock != 0 || 8921279572aSDavid Xu (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT) 8931279572aSDavid Xu ) != 0) { 894374ae2a3SJeff Roberson goto nextproc; 8955074aecdSJohn Baldwin } 89623955314SAlfred Perlstein /* 89723955314SAlfred Perlstein * only aiod changes vmspace, however it will be 89823955314SAlfred Perlstein * skipped because of the if statement above checking 89923955314SAlfred Perlstein * for P_SYSTEM 90023955314SAlfred Perlstein */ 901b61ce5b0SJeff Roberson if ((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) != P_INMEM) 902374ae2a3SJeff Roberson goto nextproc; 90369b40456SJohn Baldwin 904e602ba25SJulian Elischer switch (p->p_state) { 9050d94caffSDavid Greenman default: 906e602ba25SJulian Elischer /* Don't swap out processes in any sort 907e602ba25SJulian Elischer * of 'special' state. */ 9088f887403SJohn Baldwin break; 909df8bae1dSRodney W. Grimes 910e602ba25SJulian Elischer case PRS_NORMAL: 91126f9a767SRodney W. Grimes /* 912bfbfac11SDavid Greenman * do not swapout a realtime process 913b40ce416SJulian Elischer * Check all the thread groups.. 914bfbfac11SDavid Greenman */ 9158460a577SJohn Birrell FOREACH_THREAD_IN_PROC(p, td) { 916b61ce5b0SJeff Roberson thread_lock(td); 917b61ce5b0SJeff Roberson if (PRI_IS_REALTIME(td->td_pri_class)) { 918b61ce5b0SJeff Roberson thread_unlock(td); 919b40ce416SJulian Elischer goto nextproc; 920b61ce5b0SJeff Roberson } 921258853abSJeff Roberson slptime = (ticks - td->td_slptick) / hz; 922bfbfac11SDavid Greenman /* 9239eb881f8SSeigo Tanimura * Guarantee swap_idle_threshold1 924ceb0cf87SJohn Dyson * time in memory. 9250d94caffSDavid Greenman */ 926258853abSJeff Roberson if (slptime < swap_idle_threshold1) { 927b61ce5b0SJeff Roberson thread_unlock(td); 928b40ce416SJulian Elischer goto nextproc; 929b61ce5b0SJeff Roberson } 9309eb881f8SSeigo Tanimura 9311d7b9ed2SJulian Elischer /* 9329eb881f8SSeigo Tanimura * Do not swapout a process if it is 9339eb881f8SSeigo Tanimura * waiting on a critical event of some 9349eb881f8SSeigo Tanimura * kind or there is a thread whose 9359eb881f8SSeigo Tanimura * pageable memory may be accessed. 9361d7b9ed2SJulian Elischer * 9371d7b9ed2SJulian Elischer * This could be refined to support 9381d7b9ed2SJulian Elischer * swapping out a thread. 9391d7b9ed2SJulian Elischer */ 940c5aa6b58SJeff Roberson if (!thread_safetoswapout(td)) { 941b61ce5b0SJeff Roberson thread_unlock(td); 942e602ba25SJulian Elischer goto nextproc; 943b61ce5b0SJeff Roberson } 944ceb0cf87SJohn Dyson /* 945b40ce416SJulian Elischer * If the system is under memory stress, 946b40ce416SJulian Elischer * or if we are swapping 947b40ce416SJulian Elischer * idle processes >= swap_idle_threshold2, 948b40ce416SJulian Elischer * then swap the process out. 949ceb0cf87SJohn Dyson */ 950ceb0cf87SJohn Dyson if (((action & VM_SWAP_NORMAL) == 0) && 951ceb0cf87SJohn Dyson (((action & VM_SWAP_IDLE) == 0) || 952258853abSJeff Roberson (slptime < swap_idle_threshold2))) { 953b61ce5b0SJeff Roberson thread_unlock(td); 954b40ce416SJulian Elischer goto nextproc; 955b61ce5b0SJeff Roberson } 9569eb881f8SSeigo Tanimura 957258853abSJeff Roberson if (minslptime > slptime) 958258853abSJeff Roberson minslptime = slptime; 959b61ce5b0SJeff Roberson thread_unlock(td); 960b40ce416SJulian Elischer } 9610d94caffSDavid Greenman 96211b224dcSDavid Greenman /* 96317d9d0d0SDavid Schultz * If the pageout daemon didn't free enough pages, 96417d9d0d0SDavid Schultz * or if this process is idle and the system is 96517d9d0d0SDavid Schultz * configured to swap proactively, swap it out. 96611b224dcSDavid Greenman */ 967ceb0cf87SJohn Dyson if ((action & VM_SWAP_NORMAL) || 968ceb0cf87SJohn Dyson ((action & VM_SWAP_IDLE) && 969b40ce416SJulian Elischer (minslptime > swap_idle_threshold2))) { 970b61ce5b0SJeff Roberson if (swapout(p) == 0) 971df8bae1dSRodney W. Grimes didswap++; 972664f718bSJohn Baldwin PROC_UNLOCK(p); 9739eb881f8SSeigo Tanimura vm_map_unlock(&vm->vm_map); 9749eb881f8SSeigo Tanimura vmspace_free(vm); 9759eb881f8SSeigo Tanimura sx_sunlock(&allproc_lock); 9760d94caffSDavid Greenman goto retry; 977c96d52a9SJohn Baldwin } 9788f887403SJohn Baldwin } 979374ae2a3SJeff Roberson nextproc: 9809eb881f8SSeigo Tanimura PROC_UNLOCK(p); 9819eb881f8SSeigo Tanimura vm_map_unlock(&vm->vm_map); 9829eb881f8SSeigo Tanimura nextproc1: 9839eb881f8SSeigo Tanimura vmspace_free(vm); 98430171114SPeter Wemm continue; 985ceb0cf87SJohn Dyson } 9861005a129SJohn Baldwin sx_sunlock(&allproc_lock); 98726f9a767SRodney W. Grimes /* 98826f9a767SRodney W. Grimes * If we swapped something out, and another process needed memory, 98926f9a767SRodney W. Grimes * then wakeup the sched process. 99026f9a767SRodney W. Grimes */ 9910d94caffSDavid Greenman if (didswap) 99224a1cce3SDavid Greenman wakeup(&proc0); 993df8bae1dSRodney W. Grimes } 994df8bae1dSRodney W. Grimes 995f708ef1bSPoul-Henning Kamp static void 996b61ce5b0SJeff Roberson swapclear(p) 997b61ce5b0SJeff Roberson struct proc *p; 998b61ce5b0SJeff Roberson { 999b61ce5b0SJeff Roberson struct thread *td; 1000b61ce5b0SJeff Roberson 1001b61ce5b0SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 1002b61ce5b0SJeff Roberson 1003b61ce5b0SJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 1004b61ce5b0SJeff Roberson thread_lock(td); 1005b61ce5b0SJeff Roberson td->td_flags |= TDF_INMEM; 1006b61ce5b0SJeff Roberson td->td_flags &= ~TDF_SWAPINREQ; 1007b61ce5b0SJeff Roberson TD_CLR_SWAPPED(td); 1008b61ce5b0SJeff Roberson if (TD_CAN_RUN(td)) 1009da7bbd2cSJohn Baldwin if (setrunnable(td)) { 1010da7bbd2cSJohn Baldwin #ifdef INVARIANTS 1011da7bbd2cSJohn Baldwin /* 1012da7bbd2cSJohn Baldwin * XXX: We just cleared TDI_SWAPPED 1013da7bbd2cSJohn Baldwin * above and set TDF_INMEM, so this 1014da7bbd2cSJohn Baldwin * should never happen. 1015da7bbd2cSJohn Baldwin */ 1016da7bbd2cSJohn Baldwin panic("not waking up swapper"); 1017da7bbd2cSJohn Baldwin #endif 1018da7bbd2cSJohn Baldwin } 1019b61ce5b0SJeff Roberson thread_unlock(td); 1020b61ce5b0SJeff Roberson } 1021b61ce5b0SJeff Roberson p->p_flag &= ~(P_SWAPPINGIN|P_SWAPPINGOUT); 1022b61ce5b0SJeff Roberson p->p_flag |= P_INMEM; 1023b61ce5b0SJeff Roberson } 1024b61ce5b0SJeff Roberson 1025b61ce5b0SJeff Roberson static int 1026df8bae1dSRodney W. Grimes swapout(p) 102754d92145SMatthew Dillon struct proc *p; 1028df8bae1dSRodney W. Grimes { 1029b40ce416SJulian Elischer struct thread *td; 1030df8bae1dSRodney W. Grimes 1031ea754954SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1032d3a34985SJohn Dyson #if defined(SWAP_DEBUG) 1033d3a34985SJohn Dyson printf("swapping out %d\n", p->p_pid); 1034d3a34985SJohn Dyson #endif 10351d7b9ed2SJulian Elischer 10361d7b9ed2SJulian Elischer /* 10379eb881f8SSeigo Tanimura * The states of this process and its threads may have changed 10389eb881f8SSeigo Tanimura * by now. Assuming that there is only one pageout daemon thread, 10399eb881f8SSeigo Tanimura * this process should still be in memory. 10409eb881f8SSeigo Tanimura */ 1041b61ce5b0SJeff Roberson KASSERT((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) == P_INMEM, 10429eb881f8SSeigo Tanimura ("swapout: lost a swapout race?")); 10439eb881f8SSeigo Tanimura 1044df8bae1dSRodney W. Grimes /* 104526f9a767SRodney W. Grimes * remember the process resident count 1046df8bae1dSRodney W. Grimes */ 1047b1028ad1SLuoqi Chen p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 1048b61ce5b0SJeff Roberson /* 1049b61ce5b0SJeff Roberson * Check and mark all threads before we proceed. 1050b61ce5b0SJeff Roberson */ 1051b61ce5b0SJeff Roberson p->p_flag &= ~P_INMEM; 1052b61ce5b0SJeff Roberson p->p_flag |= P_SWAPPINGOUT; 1053982d11f8SJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 1054982d11f8SJeff Roberson thread_lock(td); 1055b61ce5b0SJeff Roberson if (!thread_safetoswapout(td)) { 1056b61ce5b0SJeff Roberson thread_unlock(td); 1057b61ce5b0SJeff Roberson swapclear(p); 1058b61ce5b0SJeff Roberson return (EBUSY); 1059b61ce5b0SJeff Roberson } 1060b61ce5b0SJeff Roberson td->td_flags &= ~TDF_INMEM; 1061664f718bSJohn Baldwin TD_SET_SWAPPED(td); 1062982d11f8SJeff Roberson thread_unlock(td); 1063982d11f8SJeff Roberson } 1064b61ce5b0SJeff Roberson td = FIRST_THREAD_IN_PROC(p); 1065b61ce5b0SJeff Roberson ++td->td_ru.ru_nswap; 1066b61ce5b0SJeff Roberson PROC_UNLOCK(p); 106726f9a767SRodney W. Grimes 1068b61ce5b0SJeff Roberson /* 1069b61ce5b0SJeff Roberson * This list is stable because all threads are now prevented from 1070b61ce5b0SJeff Roberson * running. The list is only modified in the context of a running 1071b61ce5b0SJeff Roberson * thread in this process. 1072b61ce5b0SJeff Roberson */ 1073664f718bSJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) 107449a2507bSAlan Cox vm_thread_swapout(td); 1075664f718bSJohn Baldwin 1076664f718bSJohn Baldwin PROC_LOCK(p); 1077b61ce5b0SJeff Roberson p->p_flag &= ~P_SWAPPINGOUT; 1078258853abSJeff Roberson p->p_swtick = ticks; 1079b61ce5b0SJeff Roberson return (0); 1080df8bae1dSRodney W. Grimes } 10815afce282SDavid Greenman #endif /* !NO_SWAPPING */ 1082