vm_glue.c revision 118234
167754Smsmith/* 267754Smsmith * Copyright (c) 1991, 1993 377424Smsmith * The Regents of the University of California. All rights reserved. 467754Smsmith * 567754Smsmith * This code is derived from software contributed to Berkeley by 667754Smsmith * The Mach Operating System project at Carnegie-Mellon University. 7316303Sjkim * 8316303Sjkim * Redistribution and use in source and binary forms, with or without 9316303Sjkim * modification, are permitted provided that the following conditions 10316303Sjkim * are met: 11316303Sjkim * 1. Redistributions of source code must retain the above copyright 1270243Smsmith * notice, this list of conditions and the following disclaimer. 1367754Smsmith * 2. Redistributions in binary form must reproduce the above copyright 14316303Sjkim * notice, this list of conditions and the following disclaimer in the 15316303Sjkim * documentation and/or other materials provided with the distribution. 16316303Sjkim * 3. All advertising materials mentioning features or use of this software 17316303Sjkim * must display the following acknowledgement: 18316303Sjkim * This product includes software developed by the University of 19316303Sjkim * California, Berkeley and its contributors. 20316303Sjkim * 4. Neither the name of the University nor the names of its contributors 21316303Sjkim * may be used to endorse or promote products derived from this software 22316303Sjkim * without specific prior written permission. 23316303Sjkim * 24316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25316303Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27316303Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28316303Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29316303Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30316303Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31316303Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32316303Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33316303Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34316303Sjkim * SUCH DAMAGE. 35316303Sjkim * 36316303Sjkim * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 37316303Sjkim * 38316303Sjkim * 39316303Sjkim * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40316303Sjkim * All rights reserved. 41316303Sjkim * 42316303Sjkim * Permission to use, copy, modify and distribute this software and 43316303Sjkim * its documentation is hereby granted, provided that both the copyright 44316303Sjkim * notice and this permission notice appear in all copies of the 45316303Sjkim * software, derivative works or modified versions, and any portions 46316303Sjkim * thereof, and that both notices appear in supporting documentation. 47316303Sjkim * 48316303Sjkim * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49316303Sjkim * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50316303Sjkim * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51316303Sjkim * 52316303Sjkim * Carnegie Mellon requests users of this software to return to 53316303Sjkim * 54316303Sjkim * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55316303Sjkim * School of Computer Science 56316303Sjkim * Carnegie Mellon University 57316303Sjkim * Pittsburgh PA 15213-3890 58316303Sjkim * 59316303Sjkim * any improvements or extensions that they make and grant Carnegie the 60316303Sjkim * rights to redistribute these changes. 61316303Sjkim */ 62316303Sjkim 63316303Sjkim#include <sys/cdefs.h> 64316303Sjkim__FBSDID("$FreeBSD: head/sys/vm/vm_glue.c 118234 2003-07-31 01:25:05Z peter $"); 65316303Sjkim 66316303Sjkim#include "opt_vm.h" 67316303Sjkim#include "opt_kstack_pages.h" 68316303Sjkim#include "opt_kstack_max_pages.h" 69316303Sjkim 70316303Sjkim#include <sys/param.h> 71316303Sjkim#include <sys/systm.h> 72316303Sjkim#include <sys/limits.h> 73316303Sjkim#include <sys/lock.h> 74316303Sjkim#include <sys/mutex.h> 75316303Sjkim#include <sys/proc.h> 76316303Sjkim#include <sys/resourcevar.h> 77316303Sjkim#include <sys/shm.h> 78316303Sjkim#include <sys/vmmeter.h> 79316303Sjkim#include <sys/sx.h> 80316303Sjkim#include <sys/sysctl.h> 81316303Sjkim 82316303Sjkim#include <sys/kernel.h> 83316303Sjkim#include <sys/ktr.h> 84316303Sjkim#include <sys/unistd.h> 85316303Sjkim 86316303Sjkim#include <vm/vm.h> 87316303Sjkim#include <vm/vm_param.h> 88316303Sjkim#include <vm/pmap.h> 89316303Sjkim#include <vm/vm_map.h> 90316303Sjkim#include <vm/vm_page.h> 91316303Sjkim#include <vm/vm_pageout.h> 92316303Sjkim#include <vm/vm_object.h> 93316303Sjkim#include <vm/vm_kern.h> 94316303Sjkim#include <vm/vm_extern.h> 95316303Sjkim#include <vm/vm_pager.h> 96316303Sjkim#include <vm/swap_pager.h> 97316303Sjkim 98316303Sjkim#include <sys/user.h> 99316303Sjkim 100316303Sjkimextern int maxslp; 101316303Sjkim 102316303Sjkim/* 103316303Sjkim * System initialization 104316303Sjkim * 105316303Sjkim * Note: proc0 from proc.h 106316303Sjkim */ 107316303Sjkimstatic void vm_init_limits(void *); 108316303SjkimSYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0) 109316303Sjkim 110316303Sjkim/* 111316303Sjkim * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 112316303Sjkim * 113316303Sjkim * Note: run scheduling should be divorced from the vm system. 114316303Sjkim */ 115316303Sjkimstatic void scheduler(void *); 116316303SjkimSYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 117316303Sjkim 118316303Sjkim#ifndef NO_SWAPPING 119217365Sjkimstatic void swapout(struct proc *); 120217365Sjkimstatic void vm_proc_swapin(struct proc *p); 121217365Sjkimstatic void vm_proc_swapout(struct proc *p); 122217365Sjkim#endif 123217365Sjkim 124217365Sjkim/* 125217365Sjkim * MPSAFE 126217365Sjkim * 127217365Sjkim * WARNING! This code calls vm_map_check_protection() which only checks 128217365Sjkim * the associated vm_map_entry range. It does not determine whether the 129217365Sjkim * contents of the memory is actually readable or writable. In most cases 130217365Sjkim * just checking the vm_map_entry is sufficient within the kernel's address 131217365Sjkim * space. 132217365Sjkim */ 13367754Smsmithint 134316303Sjkimkernacc(addr, len, rw) 135316303Sjkim void *addr; 136316303Sjkim int len, rw; 137316303Sjkim{ 138316303Sjkim boolean_t rv; 139316303Sjkim vm_offset_t saddr, eaddr; 140316303Sjkim vm_prot_t prot; 141316303Sjkim 142316303Sjkim KASSERT((rw & ~VM_PROT_ALL) == 0, 143316303Sjkim ("illegal ``rw'' argument to kernacc (%x)\n", rw)); 144316303Sjkim prot = rw; 145316303Sjkim saddr = trunc_page((vm_offset_t)addr); 146316303Sjkim eaddr = round_page((vm_offset_t)addr + len); 147217365Sjkim rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 148217365Sjkim return (rv == TRUE); 14967754Smsmith} 150316303Sjkim 15167754Smsmith/* 15277424Smsmith * MPSAFE 15377424Smsmith * 15467754Smsmith * WARNING! This code calls vm_map_check_protection() which only checks 15567754Smsmith * the associated vm_map_entry range. It does not determine whether the 156167802Sjkim * contents of the memory is actually readable or writable. vmapbuf(), 157228110Sjkim * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be 158167802Sjkim * used in conjuction with this call. 159167802Sjkim */ 160167802Sjkimint 161272444Sjkimuseracc(addr, len, rw) 162167802Sjkim void *addr; 163167802Sjkim int len, rw; 164167802Sjkim{ 165167802Sjkim boolean_t rv; 166167802Sjkim vm_prot_t prot; 167167802Sjkim vm_map_t map; 168167802Sjkim 169167802Sjkim KASSERT((rw & ~VM_PROT_ALL) == 0, 170167802Sjkim ("illegal ``rw'' argument to useracc (%x)\n", rw)); 171167802Sjkim prot = rw; 172167802Sjkim map = &curproc->p_vmspace->vm_map; 173167802Sjkim if ((vm_offset_t)addr + len > vm_map_max(map) || 174167802Sjkim (vm_offset_t)addr + len < (vm_offset_t)addr) { 175167802Sjkim return (FALSE); 176167802Sjkim } 177167802Sjkim rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr), 178167802Sjkim round_page((vm_offset_t)addr + len), prot); 179167802Sjkim return (rv == TRUE); 180167802Sjkim} 181228110Sjkim 182228110Sjkim/* 183228110Sjkim * MPSAFE 184228110Sjkim */ 185228110Sjkimvoid 186228110Sjkimvslock(addr, len) 187228110Sjkim void *addr; 188228110Sjkim u_int len; 189228110Sjkim{ 190228110Sjkim 191228110Sjkim vm_map_wire(&curproc->p_vmspace->vm_map, trunc_page((vm_offset_t)addr), 192228110Sjkim round_page((vm_offset_t)addr + len), FALSE); 193228110Sjkim} 194228110Sjkim 195228110Sjkim/* 196228110Sjkim * MPSAFE 197322877Sjkim */ 198167802Sjkimvoid 199167802Sjkimvsunlock(addr, len) 200250838Sjkim void *addr; 201250838Sjkim u_int len; 202250838Sjkim{ 203250838Sjkim 204250838Sjkim vm_map_unwire(&curproc->p_vmspace->vm_map, 205250838Sjkim trunc_page((vm_offset_t)addr), 206250838Sjkim round_page((vm_offset_t)addr + len), FALSE); 207250838Sjkim} 208250838Sjkim 209250838Sjkim/* 210250838Sjkim * Create the U area for a new process. 211250838Sjkim * This routine directly affects the fork perf for a process. 212250838Sjkim */ 213250838Sjkimvoid 214250838Sjkimvm_proc_new(struct proc *p) 215250838Sjkim{ 216250838Sjkim vm_page_t ma[UAREA_PAGES]; 217250838Sjkim vm_object_t upobj; 218250838Sjkim vm_offset_t up; 219250838Sjkim vm_page_t m; 220250838Sjkim u_int i; 221250838Sjkim 222250838Sjkim /* 223250838Sjkim * Allocate object for the upage. 224250838Sjkim */ 225250838Sjkim upobj = vm_object_allocate(OBJT_DEFAULT, UAREA_PAGES); 226250838Sjkim p->p_upages_obj = upobj; 227300879Sjkim 228250838Sjkim /* 229300879Sjkim * Get a kernel virtual address for the U area for this process. 230300879Sjkim */ 231250838Sjkim up = kmem_alloc_nofault(kernel_map, UAREA_PAGES * PAGE_SIZE); 232300879Sjkim if (up == 0) 233300879Sjkim panic("vm_proc_new: upage allocation failed"); 234250838Sjkim p->p_uarea = (struct user *)up; 235300879Sjkim 236300879Sjkim for (i = 0; i < UAREA_PAGES; i++) { 237250838Sjkim /* 238300879Sjkim * Get a uarea page. 239250838Sjkim */ 240300879Sjkim m = vm_page_grab(upobj, i, 241250838Sjkim VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED); 242300879Sjkim ma[i] = m; 243300879Sjkim 244250838Sjkim vm_page_lock_queues(); 245300879Sjkim vm_page_wakeup(m); 246250838Sjkim vm_page_flag_clear(m, PG_ZERO); 247250838Sjkim m->valid = VM_PAGE_BITS_ALL; 248250838Sjkim vm_page_unlock_queues(); 249250838Sjkim } 250250838Sjkim 251250838Sjkim /* 252250838Sjkim * Enter the pages into the kernel address space. 253250838Sjkim */ 254167802Sjkim pmap_qenter(up, ma, UAREA_PAGES); 255167802Sjkim} 256167802Sjkim 257167802Sjkim/* 258167802Sjkim * Dispose the U area for a process that has exited. 259167802Sjkim * This routine directly impacts the exit perf of a process. 260167802Sjkim * XXX proc_zone is marked UMA_ZONE_NOFREE, so this should never be called. 26173561Smsmith */ 262167802Sjkimvoid 263167802Sjkimvm_proc_dispose(struct proc *p) 264167802Sjkim{ 265167802Sjkim vm_object_t upobj; 266167802Sjkim vm_offset_t up; 267245582Sjkim vm_page_t m; 268167802Sjkim 269167802Sjkim upobj = p->p_upages_obj; 27073561Smsmith VM_OBJECT_LOCK(upobj); 27173561Smsmith if (upobj->resident_page_count != UAREA_PAGES) 27273561Smsmith panic("vm_proc_dispose: incorrect number of pages in upobj"); 27373561Smsmith vm_page_lock_queues(); 27473561Smsmith while ((m = TAILQ_FIRST(&upobj->memq)) != NULL) { 27573561Smsmith vm_page_busy(m); 27673561Smsmith vm_page_unwire(m, 0); 27773561Smsmith vm_page_free(m); 27873561Smsmith } 27991116Smsmith vm_page_unlock_queues(); 28073561Smsmith VM_OBJECT_UNLOCK(upobj); 28173561Smsmith up = (vm_offset_t)p->p_uarea; 282114237Snjl pmap_qremove(up, UAREA_PAGES); 28373561Smsmith kmem_free(kernel_map, up, UAREA_PAGES * PAGE_SIZE); 28473561Smsmith vm_object_deallocate(upobj); 285249112Sjkim} 286249112Sjkim 28767754Smsmith#ifndef NO_SWAPPING 28867754Smsmith/* 28967754Smsmith * Allow the U area for a process to be prejudicially paged out. 29077424Smsmith */ 29167754Smsmithstatic void 29267754Smsmithvm_proc_swapout(struct proc *p) 29367754Smsmith{ 29467754Smsmith vm_object_t upobj; 29567754Smsmith vm_offset_t up; 29667754Smsmith vm_page_t m; 297285797Sjkim 29867754Smsmith upobj = p->p_upages_obj; 299298714Sjkim VM_OBJECT_LOCK(upobj); 300298714Sjkim if (upobj->resident_page_count != UAREA_PAGES) 301298714Sjkim panic("vm_proc_dispose: incorrect number of pages in upobj"); 302298714Sjkim vm_page_lock_queues(); 303298714Sjkim TAILQ_FOREACH(m, &upobj->memq, listq) { 304298714Sjkim vm_page_dirty(m); 305298714Sjkim vm_page_unwire(m, 0); 306298714Sjkim } 307298714Sjkim vm_page_unlock_queues(); 308298714Sjkim VM_OBJECT_UNLOCK(upobj); 309298714Sjkim up = (vm_offset_t)p->p_uarea; 310298714Sjkim pmap_qremove(up, UAREA_PAGES); 311298714Sjkim} 312298714Sjkim 313298714Sjkim/* 314298714Sjkim * Bring the U area for a specified process back in. 315298714Sjkim */ 316298714Sjkimstatic void 317298714Sjkimvm_proc_swapin(struct proc *p) 318285797Sjkim{ 319285797Sjkim vm_page_t ma[UAREA_PAGES]; 320285797Sjkim vm_object_t upobj; 321285797Sjkim vm_offset_t up; 322285797Sjkim vm_page_t m; 323285797Sjkim int rv; 324285797Sjkim int i; 325285797Sjkim 326285797Sjkim upobj = p->p_upages_obj; 327285797Sjkim VM_OBJECT_LOCK(upobj); 328285797Sjkim for (i = 0; i < UAREA_PAGES; i++) { 329285797Sjkim m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 330285797Sjkim if (m->valid != VM_PAGE_BITS_ALL) { 331285797Sjkim rv = vm_pager_get_pages(upobj, &m, 1, 0); 332285797Sjkim if (rv != VM_PAGER_OK) 333285797Sjkim panic("vm_proc_swapin: cannot get upage"); 334285797Sjkim } 335285797Sjkim ma[i] = m; 336316303Sjkim } 337285797Sjkim if (upobj->resident_page_count != UAREA_PAGES) 338285797Sjkim panic("vm_proc_swapin: lost pages from upobj"); 339316303Sjkim vm_page_lock_queues(); 340316303Sjkim TAILQ_FOREACH(m, &upobj->memq, listq) { 341316303Sjkim m->valid = VM_PAGE_BITS_ALL; 342316303Sjkim vm_page_wire(m); 343316303Sjkim vm_page_wakeup(m); 344316303Sjkim } 345316303Sjkim vm_page_unlock_queues(); 346285797Sjkim VM_OBJECT_UNLOCK(upobj); 347298714Sjkim up = (vm_offset_t)p->p_uarea; 348285797Sjkim pmap_qenter(up, ma, UAREA_PAGES); 349151937Sjkim} 35067754Smsmith 351193267Sjkim/* 352151937Sjkim * Swap in the UAREAs of all processes swapped out to the given device. 35367754Smsmith * The pages in the UAREA are marked dirty and their swap metadata is freed. 35467754Smsmith */ 355102550Siwasakivoid 35669746Smsmithvm_proc_swapin_all(int devidx) 357298714Sjkim{ 35877424Smsmith struct proc *p; 35967754Smsmith vm_object_t object; 36067754Smsmith vm_page_t m; 361193267Sjkim 362193267Sjkimretry: 363272444Sjkim sx_slock(&allproc_lock); 364272444Sjkim FOREACH_PROC_IN_SYSTEM(p) { 365100966Siwasaki PROC_LOCK(p); 366100966Siwasaki object = p->p_upages_obj; 367298714Sjkim if (object != NULL) { 36877424Smsmith VM_OBJECT_LOCK(object); 36991116Smsmith if (swap_pager_isswapped(object, devidx)) { 37067754Smsmith VM_OBJECT_UNLOCK(object); 371298714Sjkim sx_sunlock(&allproc_lock); 372123315Snjl faultin(p); 373123315Snjl PROC_UNLOCK(p); 374123315Snjl VM_OBJECT_LOCK(object); 375298714Sjkim vm_page_lock_queues(); 376123315Snjl TAILQ_FOREACH(m, &object->memq, listq) 377123315Snjl vm_page_dirty(m); 378123315Snjl vm_page_unlock_queues(); 379193267Sjkim swap_pager_freespace(object, 0, 380193267Sjkim object->un_pager.swp.swp_bcount); 381193267Sjkim VM_OBJECT_UNLOCK(object); 382193267Sjkim goto retry; 383298714Sjkim } 38499679Siwasaki VM_OBJECT_UNLOCK(object); 38599679Siwasaki } 38699679Siwasaki PROC_UNLOCK(p); 387298714Sjkim } 38877424Smsmith sx_sunlock(&allproc_lock); 38969746Smsmith} 39069746Smsmith#endif 391298714Sjkim 39291116Smsmith#ifndef KSTACK_MAX_PAGES 39391116Smsmith#define KSTACK_MAX_PAGES 32 39469746Smsmith#endif 395316303Sjkim 396316303Sjkim/* 397316303Sjkim * Create the kernel stack (including pcb for i386) for a new thread. 398316303Sjkim * This routine directly affects the fork perf for a process and 39999679Siwasaki * create performance for a thread. 40082367Smsmith */ 401202771Sjkimvoid 40282367Smsmithvm_thread_new(struct thread *td, int pages) 40382367Smsmith{ 404316303Sjkim vm_object_t ksobj; 405316303Sjkim vm_offset_t ks; 406316303Sjkim vm_page_t m, ma[KSTACK_MAX_PAGES]; 407316303Sjkim int i; 408316303Sjkim 409272444Sjkim /* Bounds check */ 410272444Sjkim if (pages <= 1) 411272444Sjkim pages = KSTACK_PAGES; 412272444Sjkim else if (pages > KSTACK_MAX_PAGES) 41367754Smsmith pages = KSTACK_MAX_PAGES; 41477424Smsmith /* 41591116Smsmith * Allocate an object for the kstack. 41667754Smsmith */ 41767754Smsmith ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 418151937Sjkim td->td_kstack_obj = ksobj; 419151937Sjkim /* 420151937Sjkim * Get a kernel virtual address for this thread's kstack. 421151937Sjkim */ 422151937Sjkim ks = kmem_alloc_nofault(kernel_map, 423151937Sjkim (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 42467754Smsmith if (ks == 0) 425151937Sjkim panic("vm_thread_new: kstack allocation failed"); 426151937Sjkim if (KSTACK_GUARD_PAGES != 0) { 427151937Sjkim pmap_qremove(ks, KSTACK_GUARD_PAGES); 428151937Sjkim ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 429151937Sjkim } 43067754Smsmith td->td_kstack = ks; 431151937Sjkim /* 43267754Smsmith * Knowing the number of pages allocated is useful when you 43367754Smsmith * want to deallocate them. 43477424Smsmith */ 43567754Smsmith td->td_kstack_pages = pages; 43667754Smsmith /* 43767754Smsmith * For the length of the stack, link in a real page of ram for each 43867754Smsmith * page of stack. 43967754Smsmith */ 44067754Smsmith VM_OBJECT_LOCK(ksobj); 44177424Smsmith for (i = 0; i < pages; i++) { 44267754Smsmith /* 44367754Smsmith * Get a kernel stack page. 44467754Smsmith */ 44567754Smsmith m = vm_page_grab(ksobj, i, 44667754Smsmith VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED); 44777424Smsmith ma[i] = m; 44867754Smsmith vm_page_lock_queues(); 44967754Smsmith vm_page_wakeup(m); 45067754Smsmith m->valid = VM_PAGE_BITS_ALL; 45167754Smsmith vm_page_unlock_queues(); 45277424Smsmith } 45367754Smsmith VM_OBJECT_UNLOCK(ksobj); 45484491Smsmith pmap_qenter(ks, ma, pages); 45567754Smsmith} 45667754Smsmith 45777424Smsmith/* 45867754Smsmith * Dispose of a thread's kernel stack. 45967754Smsmith */ 46067754Smsmithvoid 46167754Smsmithvm_thread_dispose(struct thread *td) 46291116Smsmith{ 46391116Smsmith vm_object_t ksobj; 46491116Smsmith vm_offset_t ks; 46591116Smsmith vm_page_t m; 46691116Smsmith int i, pages; 46791116Smsmith 46867754Smsmith pages = td->td_kstack_pages; 469151937Sjkim ksobj = td->td_kstack_obj; 47067754Smsmith ks = td->td_kstack; 47167754Smsmith pmap_qremove(ks, pages); 47277424Smsmith VM_OBJECT_LOCK(ksobj); 47367754Smsmith for (i = 0; i < pages; i++) { 47467754Smsmith m = vm_page_lookup(ksobj, i); 47567754Smsmith if (m == NULL) 47667754Smsmith panic("vm_thread_dispose: kstack already missing?"); 47767754Smsmith vm_page_lock_queues(); 478151937Sjkim vm_page_busy(m); 47967754Smsmith vm_page_unwire(m, 0); 48083174Smsmith vm_page_free(m); 48183174Smsmith vm_page_unlock_queues(); 48267754Smsmith } 48367754Smsmith VM_OBJECT_UNLOCK(ksobj); 48467754Smsmith vm_object_deallocate(ksobj); 48583174Smsmith kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 48683174Smsmith (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 48767754Smsmith} 48867754Smsmith 48983174Smsmith/* 49067754Smsmith * Allow a thread's kernel stack to be paged out. 491151937Sjkim */ 492193267Sjkimvoid 493151937Sjkimvm_thread_swapout(struct thread *td) 49467754Smsmith{ 49567754Smsmith vm_object_t ksobj; 49683174Smsmith vm_page_t m; 49767754Smsmith int i, pages; 498151937Sjkim 499193267Sjkim#ifdef __alpha__ 500151937Sjkim /* 501298714Sjkim * Make sure we aren't fpcurthread. 50267754Smsmith */ 50367754Smsmith alpha_fpstate_save(td, 1); 50483174Smsmith#endif 50567754Smsmith pages = td->td_kstack_pages; 506151937Sjkim ksobj = td->td_kstack_obj; 507193267Sjkim pmap_qremove(td->td_kstack, pages); 508151937Sjkim VM_OBJECT_LOCK(ksobj); 50967754Smsmith for (i = 0; i < pages; i++) { 51067754Smsmith m = vm_page_lookup(ksobj, i); 51167754Smsmith if (m == NULL) 51283174Smsmith panic("vm_thread_swapout: kstack already missing?"); 51367754Smsmith vm_page_lock_queues(); 514151937Sjkim vm_page_dirty(m); 515193267Sjkim vm_page_unwire(m, 0); 516151937Sjkim vm_page_unlock_queues(); 517298714Sjkim } 51867754Smsmith VM_OBJECT_UNLOCK(ksobj); 51967754Smsmith} 52083174Smsmith 52167754Smsmith/* 522151937Sjkim * Bring the kernel stack for a specified thread back in. 523193267Sjkim */ 524151937Sjkimvoid 52567754Smsmithvm_thread_swapin(struct thread *td) 52667754Smsmith{ 52783174Smsmith vm_object_t ksobj; 52867754Smsmith vm_page_t m, ma[KSTACK_MAX_PAGES]; 529151937Sjkim int i, pages, rv; 530193267Sjkim 531151937Sjkim pages = td->td_kstack_pages; 53267754Smsmith ksobj = td->td_kstack_obj; 53367754Smsmith VM_OBJECT_LOCK(ksobj); 53467754Smsmith for (i = 0; i < pages; i++) { 53583174Smsmith m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 53667754Smsmith if (m->valid != VM_PAGE_BITS_ALL) { 537151937Sjkim rv = vm_pager_get_pages(ksobj, &m, 1, 0); 538193267Sjkim if (rv != VM_PAGER_OK) 539151937Sjkim panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid); 540202771Sjkim m = vm_page_lookup(ksobj, i); 54167754Smsmith m->valid = VM_PAGE_BITS_ALL; 54267754Smsmith } 54383174Smsmith ma[i] = m; 54467754Smsmith vm_page_lock_queues(); 545151937Sjkim vm_page_wire(m); 546193267Sjkim vm_page_wakeup(m); 547151937Sjkim vm_page_unlock_queues(); 54867754Smsmith } 54967754Smsmith VM_OBJECT_UNLOCK(ksobj); 55067754Smsmith pmap_qenter(td->td_kstack, ma, pages); 551298714Sjkim#ifdef __alpha__ 552298714Sjkim /* 553298714Sjkim * The pcb may be at a different physical address now so cache the 554298714Sjkim * new address. 555298714Sjkim */ 556298714Sjkim td->td_md.md_pcbpaddr = (void *)vtophys((vm_offset_t)td->td_pcb); 557298714Sjkim#endif 558298714Sjkim} 559241973Sjkim 560167802Sjkim/* 561167802Sjkim * Set up a variable-sized alternate kstack. 562167802Sjkim */ 563237412Sjkimvoid 56467754Smsmithvm_thread_new_altkstack(struct thread *td, int pages) 56567754Smsmith{ 566241973Sjkim 567167802Sjkim td->td_altkstack = td->td_kstack; 568167802Sjkim td->td_altkstack_obj = td->td_kstack_obj; 569241973Sjkim td->td_altkstack_pages = td->td_kstack_pages; 570241973Sjkim 571167802Sjkim vm_thread_new(td, pages); 572272444Sjkim} 573167802Sjkim 574272444Sjkim/* 575272444Sjkim * Restore the original kstack. 576272444Sjkim */ 577272444Sjkimvoid 578272444Sjkimvm_thread_dispose_altkstack(struct thread *td) 579272444Sjkim{ 580272444Sjkim 581272444Sjkim vm_thread_dispose(td); 582272444Sjkim 58383174Smsmith td->td_kstack = td->td_altkstack; 584114237Snjl td->td_kstack_obj = td->td_altkstack_obj; 585167802Sjkim td->td_kstack_pages = td->td_altkstack_pages; 58667754Smsmith td->td_altkstack = 0; 58767754Smsmith td->td_altkstack_obj = NULL; 588167802Sjkim td->td_altkstack_pages = 0; 589114237Snjl} 590167802Sjkim 59167754Smsmith/* 59267754Smsmith * Implement fork's actions on an address space. 593167802Sjkim * Here we arrange for the address space to be copied or referenced, 594167802Sjkim * allocate a user struct (pcb and kernel stack), then call the 595167802Sjkim * machine-dependent layer to fill those in and make the new process 59667754Smsmith * ready to run. The new process is set up so that it returns directly 597285797Sjkim * to user mode to avoid stack copying and relocation problems. 59867754Smsmith */ 599151937Sjkimvoid 60067754Smsmithvm_forkproc(td, p2, td2, flags) 601151937Sjkim struct thread *td; 602151937Sjkim struct proc *p2; 603151937Sjkim struct thread *td2; 60467754Smsmith int flags; 60567754Smsmith{ 606151937Sjkim struct proc *p1 = td->td_proc; 60767754Smsmith struct user *up; 60867754Smsmith 60967754Smsmith GIANT_REQUIRED; 61077424Smsmith 61167754Smsmith if ((flags & RFPROC) == 0) { 61267754Smsmith /* 61367754Smsmith * Divorce the memory, if it is shared, essentially 61477424Smsmith * this changes shared memory amongst threads, into 61567754Smsmith * COW locally. 61667754Smsmith */ 61799679Siwasaki if ((flags & RFMEM) == 0) { 61877424Smsmith if (p1->p_vmspace->vm_refcnt > 1) { 61967754Smsmith vmspace_unshare(p1); 62067754Smsmith } 62167754Smsmith } 62267754Smsmith cpu_fork(td, p2, td2, flags); 623151937Sjkim return; 62467754Smsmith } 62567754Smsmith 626114237Snjl if (flags & RFMEM) { 627114237Snjl p2->p_vmspace = p1->p_vmspace; 628298714Sjkim p1->p_vmspace->vm_refcnt++; 629114237Snjl } 630114237Snjl 631114237Snjl while (vm_page_count_severe()) { 632114237Snjl VM_WAIT; 63377424Smsmith } 634298714Sjkim 63567754Smsmith if ((flags & RFMEM) == 0) { 636202771Sjkim p2->p_vmspace = vmspace_fork(p1->p_vmspace); 63767754Smsmith 63867754Smsmith pmap_pinit2(vmspace_pmap(p2->p_vmspace)); 639197104Sjkim 64067754Smsmith if (p1->p_vmspace->vm_shm) 641197104Sjkim shmfork(p1, p2); 64267754Smsmith } 64367754Smsmith 644197104Sjkim /* XXXKSE this is unsatisfactory but should be adequate */ 64587031Smsmith up = p2->p_uarea; 646197104Sjkim MPASS(p2->p_sigacts != NULL); 647197104Sjkim 648197104Sjkim /* 64987031Smsmith * p_stats currently points at fields in the user struct 650197104Sjkim * but not at &u, instead at p_addr. Copy parts of 651197104Sjkim * p_stats; zero the rest of p_stats (statistics). 652197104Sjkim */ 653197104Sjkim p2->p_stats = &up->u_stats; 65487031Smsmith bzero(&up->u_stats.pstat_startzero, 655197104Sjkim (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 65667754Smsmith (caddr_t) &up->u_stats.pstat_startzero)); 657241973Sjkim bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 65867754Smsmith ((caddr_t) &up->u_stats.pstat_endcopy - 65967754Smsmith (caddr_t) &up->u_stats.pstat_startcopy)); 66077424Smsmith 66167754Smsmith /* 662241973Sjkim * cpu_fork will copy and update the pcb, set up the kernel stack, 66367754Smsmith * and make the child ready to run. 664126372Snjl */ 665197104Sjkim cpu_fork(td, p2, td2, flags); 666126372Snjl} 667241973Sjkim 66867754Smsmith/* 669284583Sjkim * Called after process has been wait(2)'ed apon and is being reaped. 670284583Sjkim * The idea is to reclaim resources that we could not reclaim while 671284583Sjkim * the process was still executing. 672284583Sjkim */ 673197104Sjkimvoid 674284583Sjkimvm_waitproc(p) 675193267Sjkim struct proc *p; 676193267Sjkim{ 677193267Sjkim 678193267Sjkim GIANT_REQUIRED; 679193267Sjkim vmspace_exitfree(p); /* and clean-out the vmspace */ 680193267Sjkim} 68167754Smsmith 682193267Sjkim/* 683193267Sjkim * Set default limits for VM system. 684193267Sjkim * Called for proc 0, and then inherited by all others. 685193267Sjkim * 686193267Sjkim * XXX should probably act directly on proc0. 687193267Sjkim */ 688193267Sjkimstatic void 689193267Sjkimvm_init_limits(udata) 690193267Sjkim void *udata; 691193267Sjkim{ 692193267Sjkim struct proc *p = udata; 693193267Sjkim int rss_limit; 694193267Sjkim 695193267Sjkim /* 696193267Sjkim * Set up the initial limits on process VM. Set the maximum resident 697193267Sjkim * set size to be half of (reasonably) available memory. Since this 698193267Sjkim * is a soft limit, it comes into effect only when the system is out 699193267Sjkim * of memory - half of main memory helps to favor smaller processes, 700193267Sjkim * and reduces thrashing of the object cache. 701193267Sjkim */ 702193267Sjkim p->p_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 70367754Smsmith p->p_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 704151937Sjkim p->p_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 70567754Smsmith p->p_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 70683174Smsmith /* limit the limit to no less than 2MB */ 70783174Smsmith rss_limit = max(cnt.v_free_count, 512); 708193267Sjkim p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 70983174Smsmith p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 71083174Smsmith} 71191116Smsmith 71283174Smsmithvoid 71367754Smsmithfaultin(p) 71483174Smsmith struct proc *p; 715193267Sjkim{ 71667754Smsmith#ifdef NO_SWAPPING 71767754Smsmith 71867754Smsmith PROC_LOCK_ASSERT(p, MA_OWNED); 719151937Sjkim if ((p->p_sflag & PS_INMEM) == 0) 720151937Sjkim panic("faultin: proc swapped out with NO_SWAPPING!"); 72167754Smsmith#else /* !NO_SWAPPING */ 72267754Smsmith struct thread *td; 72377424Smsmith 72467754Smsmith GIANT_REQUIRED; 72567754Smsmith PROC_LOCK_ASSERT(p, MA_OWNED); 72667754Smsmith /* 72777424Smsmith * If another process is swapping in this process, 72867754Smsmith * just wait until it finishes. 72967754Smsmith */ 730107325Siwasaki if (p->p_sflag & PS_SWAPPINGIN) 731193267Sjkim msleep(&p->p_sflag, &p->p_mtx, PVM, "faultin", 0); 732193267Sjkim else if ((p->p_sflag & PS_INMEM) == 0) { 733193267Sjkim /* 734193267Sjkim * Don't let another thread swap process p out while we are 735199337Sjkim * busy swapping it in. 736199337Sjkim */ 737199337Sjkim ++p->p_lock; 738199337Sjkim mtx_lock_spin(&sched_lock); 739107325Siwasaki p->p_sflag |= PS_SWAPPINGIN; 740107325Siwasaki mtx_unlock_spin(&sched_lock); 74167754Smsmith PROC_UNLOCK(p); 742138287Smarks 743138287Smarks vm_proc_swapin(p); 744138287Smarks FOREACH_THREAD_IN_PROC(p, td) 745107325Siwasaki vm_thread_swapin(td); 74667754Smsmith 74777424Smsmith PROC_LOCK(p); 74867754Smsmith mtx_lock_spin(&sched_lock); 74991116Smsmith p->p_sflag &= ~PS_SWAPPINGIN; 75067754Smsmith p->p_sflag |= PS_INMEM; 75167754Smsmith FOREACH_THREAD_IN_PROC(p, td) { 75267754Smsmith TD_CLR_SWAPPED(td); 753210976Sjkim if (TD_CAN_RUN(td)) 754210976Sjkim setrunnable(td); 755210976Sjkim } 756210976Sjkim mtx_unlock_spin(&sched_lock); 757210976Sjkim 758210976Sjkim wakeup(&p->p_sflag); 759249112Sjkim 760210976Sjkim /* Allow other threads to swap p out now. */ 761210976Sjkim --p->p_lock; 762210976Sjkim } 763210976Sjkim#endif /* NO_SWAPPING */ 764210976Sjkim} 765210976Sjkim 766210976Sjkim/* 767210976Sjkim * This swapin algorithm attempts to swap-in processes only if there 768210976Sjkim * is enough space for them. Of course, if a process waits for a long 769210976Sjkim * time, it will be swapped in anyway. 770210976Sjkim * 771253690Sjkim * XXXKSE - process with the thread with highest priority counts.. 772253690Sjkim * 773253690Sjkim * Giant is still held at this point, to be released in tsleep. 774253690Sjkim */ 775210976Sjkim/* ARGSUSED*/ 776210976Sjkimstatic void 777210976Sjkimscheduler(dummy) 778210976Sjkim void *dummy; 779210976Sjkim{ 780210976Sjkim struct proc *p; 781210976Sjkim struct thread *td; 782210976Sjkim int pri; 783210976Sjkim struct proc *pp; 784210976Sjkim int ppri; 785249112Sjkim 786249112Sjkim mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 787249112Sjkim /* GIANT_REQUIRED */ 788249112Sjkim 789249112Sjkimloop: 790249112Sjkim if (vm_page_count_min()) { 791249112Sjkim VM_WAIT; 792249112Sjkim goto loop; 793249112Sjkim } 794249112Sjkim 795281396Sjkim pp = NULL; 796281396Sjkim ppri = INT_MIN; 797281396Sjkim sx_slock(&allproc_lock); 798281396Sjkim FOREACH_PROC_IN_SYSTEM(p) { 799281396Sjkim struct ksegrp *kg; 800281396Sjkim if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) { 801249112Sjkim continue; 802249112Sjkim } 803249112Sjkim mtx_lock_spin(&sched_lock); 804249112Sjkim FOREACH_THREAD_IN_PROC(p, td) { 805249112Sjkim /* 806249112Sjkim * An otherwise runnable thread of a process 807249112Sjkim * swapped out has only the TDI_SWAPPED bit set. 808249112Sjkim * 809249112Sjkim */ 810249112Sjkim if (td->td_inhibitors == TDI_SWAPPED) { 811249112Sjkim kg = td->td_ksegrp; 812249112Sjkim pri = p->p_swtime + kg->kg_slptime; 813249112Sjkim if ((p->p_sflag & PS_SWAPINREQ) == 0) { 814249112Sjkim pri -= kg->kg_nice * 8; 815281396Sjkim } 816249112Sjkim 817249112Sjkim /* 818249112Sjkim * if this ksegrp is higher priority 819151937Sjkim * and there is enough space, then select 82067754Smsmith * this process instead of the previous 82167754Smsmith * selection. 82277424Smsmith */ 82367754Smsmith if (pri > ppri) { 82467754Smsmith pp = p; 82567754Smsmith ppri = pri; 82667754Smsmith } 82777424Smsmith } 82867754Smsmith } 82967754Smsmith mtx_unlock_spin(&sched_lock); 83067754Smsmith } 83167754Smsmith sx_sunlock(&allproc_lock); 83277424Smsmith 83367754Smsmith /* 83467754Smsmith * Nothing to do, back to sleep. 83587031Smsmith */ 83687031Smsmith if ((p = pp) == NULL) { 83787031Smsmith tsleep(&proc0, PVM, "sched", maxslp * hz / 2); 83887031Smsmith goto loop; 83967754Smsmith } 84077424Smsmith PROC_LOCK(p); 84167754Smsmith 84267754Smsmith /* 84367754Smsmith * Another process may be bringing or may have already 84473561Smsmith * brought this process in while we traverse all threads. 84577424Smsmith * Or, this process may even be being swapped out again. 84673561Smsmith */ 84773561Smsmith if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) { 848322877Sjkim PROC_UNLOCK(p); 84973561Smsmith goto loop; 85067754Smsmith } 85177424Smsmith 85267754Smsmith mtx_lock_spin(&sched_lock); 85367754Smsmith p->p_sflag &= ~PS_SWAPINREQ; 85467754Smsmith mtx_unlock_spin(&sched_lock); 85567754Smsmith 85667754Smsmith /* 85777424Smsmith * We would like to bring someone in. (only if there is space). 85867754Smsmith * [What checks the space? ] 85967754Smsmith */ 86067754Smsmith faultin(p); 86177424Smsmith PROC_UNLOCK(p); 86267754Smsmith mtx_lock_spin(&sched_lock); 86367754Smsmith p->p_swtime = 0; 86467754Smsmith mtx_unlock_spin(&sched_lock); 86567754Smsmith goto loop; 866151937Sjkim} 86767754Smsmith 86884491Smsmith#ifndef NO_SWAPPING 86984491Smsmith 870202771Sjkim/* 871202771Sjkim * Swap_idle_threshold1 is the guaranteed swapped in time for a process 872202771Sjkim */ 873202771Sjkimstatic int swap_idle_threshold1 = 2; 87484491SmsmithSYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW, 87584491Smsmith &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process"); 87684491Smsmith 877202771Sjkim/* 87884491Smsmith * Swap_idle_threshold2 is the time that a process can be idle before 879202771Sjkim * it will be swapped out, if idle swapping is enabled. 88084491Smsmith */ 88184491Smsmithstatic int swap_idle_threshold2 = 10; 882322877SjkimSYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW, 883322877Sjkim &swap_idle_threshold2, 0, "Time before a process will be swapped out"); 884322877Sjkim 885322877Sjkim/* 886322877Sjkim * Swapout is driven by the pageout daemon. Very simple, we find eligible 887246849Sjkim * procs and unwire their u-areas. We try to always "swap" at least one 888322877Sjkim * process in case we need the room for a swapin. 889322877Sjkim * If any procs have been sleeping/stopped for at least maxslp seconds, 890322877Sjkim * they are swapped. Else, we swap the longest-sleeping or stopped process, 891322877Sjkim * if any, otherwise the longest-resident process. 892322877Sjkim */ 893322877Sjkimvoid 894322877Sjkimswapout_procs(action) 895322877Sjkimint action; 896322877Sjkim{ 897322877Sjkim struct proc *p; 898322877Sjkim struct thread *td; 899322877Sjkim struct ksegrp *kg; 900322877Sjkim int didswap = 0; 901151937Sjkim 902151937Sjkim GIANT_REQUIRED; 903151937Sjkim 904246849Sjkimretry: 905167802Sjkim sx_slock(&allproc_lock); 906167802Sjkim FOREACH_PROC_IN_SYSTEM(p) { 907167802Sjkim struct vmspace *vm; 908167802Sjkim int minslptime = 100000; 909197104Sjkim 910197104Sjkim /* 911197104Sjkim * Watch out for a process in 912287168Sjkim * creation. It may have no 913197104Sjkim * address space or lock yet. 914167802Sjkim */ 915167802Sjkim mtx_lock_spin(&sched_lock); 916281396Sjkim if (p->p_state == PRS_NEW) { 917167802Sjkim mtx_unlock_spin(&sched_lock); 918151937Sjkim continue; 919151937Sjkim } 920151937Sjkim mtx_unlock_spin(&sched_lock); 921151937Sjkim 922151937Sjkim /* 923151937Sjkim * An aio daemon switches its 924151937Sjkim * address space while running. 925138287Smarks * Perform a quick check whether 926138287Smarks * a process has P_SYSTEM. 927138287Smarks */ 928138287Smarks if ((p->p_flag & P_SYSTEM) != 0) 929285797Sjkim continue; 930151937Sjkim 93199679Siwasaki /* 93299679Siwasaki * Do not swapout a process that 93399679Siwasaki * is waiting for VM data 93499679Siwasaki * structures as there is a possible 93599679Siwasaki * deadlock. Test this first as 93699679Siwasaki * this may block. 93799679Siwasaki * 938102550Siwasaki * Lock the map until swapout 93973561Smsmith * finishes, or a thread of this 94077424Smsmith * process may attempt to alter 941114237Snjl * the map. 942114237Snjl */ 943298714Sjkim PROC_LOCK(p); 94473561Smsmith vm = p->p_vmspace; 94573561Smsmith KASSERT(vm != NULL, 94673561Smsmith ("swapout_procs: a process has no address space")); 94767754Smsmith ++vm->vm_refcnt; 948245582Sjkim PROC_UNLOCK(p); 949245582Sjkim if (!vm_map_trylock(&vm->vm_map)) 950245582Sjkim goto nextproc1; 951245582Sjkim 952245582Sjkim PROC_LOCK(p); 953245582Sjkim if (p->p_lock != 0 || 954245582Sjkim (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT) 955245582Sjkim ) != 0) { 956245582Sjkim goto nextproc2; 957245582Sjkim } 958245582Sjkim /* 959245582Sjkim * only aiod changes vmspace, however it will be 960167802Sjkim * skipped because of the if statement above checking 961167802Sjkim * for P_SYSTEM 962167802Sjkim */ 963167802Sjkim if ((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) != PS_INMEM) 964243347Sjkim goto nextproc2; 965167802Sjkim 966167802Sjkim switch (p->p_state) { 967167802Sjkim default: 968245582Sjkim /* Don't swap out processes in any sort 969167802Sjkim * of 'special' state. */ 970167802Sjkim break; 971167802Sjkim 972243347Sjkim case PRS_NORMAL: 973167802Sjkim mtx_lock_spin(&sched_lock); 974167802Sjkim /* 975167802Sjkim * do not swapout a realtime process 976167802Sjkim * Check all the thread groups.. 977167802Sjkim */ 978167802Sjkim FOREACH_KSEGRP_IN_PROC(p, kg) { 979167802Sjkim if (PRI_IS_REALTIME(kg->kg_pri_class)) 980167802Sjkim goto nextproc; 981167802Sjkim 982167802Sjkim /* 983167802Sjkim * Guarantee swap_idle_threshold1 984167802Sjkim * time in memory. 985167802Sjkim */ 986167802Sjkim if (kg->kg_slptime < swap_idle_threshold1) 987167802Sjkim goto nextproc; 988167802Sjkim 989167802Sjkim /* 990167802Sjkim * Do not swapout a process if it is 991167802Sjkim * waiting on a critical event of some 992167802Sjkim * kind or there is a thread whose 993167802Sjkim * pageable memory may be accessed. 994167802Sjkim * 995167802Sjkim * This could be refined to support 996167802Sjkim * swapping out a thread. 997167802Sjkim */ 998167802Sjkim FOREACH_THREAD_IN_GROUP(kg, td) { 999245582Sjkim if ((td->td_priority) < PSOCK || 1000245582Sjkim !thread_safetoswapout(td)) 1001245582Sjkim goto nextproc; 1002245582Sjkim } 1003245582Sjkim /* 1004252279Sjkim * If the system is under memory stress, 1005245582Sjkim * or if we are swapping 1006281396Sjkim * idle processes >= swap_idle_threshold2, 1007245582Sjkim * then swap the process out. 1008245582Sjkim */ 1009245582Sjkim if (((action & VM_SWAP_NORMAL) == 0) && 1010281396Sjkim (((action & VM_SWAP_IDLE) == 0) || 1011245582Sjkim (kg->kg_slptime < swap_idle_threshold2))) 1012245582Sjkim goto nextproc; 1013245582Sjkim 1014245582Sjkim if (minslptime > kg->kg_slptime) 1015245582Sjkim minslptime = kg->kg_slptime; 1016272444Sjkim } 1017272444Sjkim 1018272444Sjkim /* 1019272444Sjkim * If the process has been asleep for awhile and had 1020272444Sjkim * most of its pages taken away already, swap it out. 1021272444Sjkim */ 1022245582Sjkim if ((action & VM_SWAP_NORMAL) || 1023272444Sjkim ((action & VM_SWAP_IDLE) && 1024272444Sjkim (minslptime > swap_idle_threshold2))) { 1025272444Sjkim swapout(p); 1026272444Sjkim didswap++; 1027272444Sjkim mtx_unlock_spin(&sched_lock); 1028298714Sjkim PROC_UNLOCK(p); 1029298714Sjkim vm_map_unlock(&vm->vm_map); 1030298714Sjkim vmspace_free(vm); 1031298714Sjkim sx_sunlock(&allproc_lock); 1032298714Sjkim goto retry; 1033298714Sjkim } 1034298714Sjkimnextproc: 1035272444Sjkim mtx_unlock_spin(&sched_lock); 1036272444Sjkim } 1037272444Sjkimnextproc2: 1038245582Sjkim PROC_UNLOCK(p); 1039151937Sjkim vm_map_unlock(&vm->vm_map); 104067754Smsmithnextproc1: 1041151937Sjkim vmspace_free(vm); 1042151937Sjkim continue; 1043151937Sjkim } 104467754Smsmith sx_sunlock(&allproc_lock); 104582367Smsmith /* 1046151937Sjkim * If we swapped something out, and another process needed memory, 1047151937Sjkim * then wakeup the sched process. 104882367Smsmith */ 1049151937Sjkim if (didswap) 1050151937Sjkim wakeup(&proc0); 1051151937Sjkim} 105282367Smsmith 105391116Smsmithstatic void 1054151937Sjkimswapout(p) 1055151937Sjkim struct proc *p; 1056151937Sjkim{ 1057151937Sjkim struct thread *td; 1058151937Sjkim 1059151937Sjkim PROC_LOCK_ASSERT(p, MA_OWNED); 1060151937Sjkim mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); 1061151937Sjkim#if defined(SWAP_DEBUG) 1062151937Sjkim printf("swapping out %d\n", p->p_pid); 1063151937Sjkim#endif 1064151937Sjkim 1065151937Sjkim /* 1066151937Sjkim * The states of this process and its threads may have changed 1067151937Sjkim * by now. Assuming that there is only one pageout daemon thread, 1068151937Sjkim * this process should still be in memory. 1069151937Sjkim */ 107091116Smsmith KASSERT((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) == PS_INMEM, 107191116Smsmith ("swapout: lost a swapout race?")); 107282367Smsmith 107391116Smsmith#if defined(INVARIANTS) 107491116Smsmith /* 107591116Smsmith * Make sure that all threads are safe to be swapped out. 107691116Smsmith * 107782367Smsmith * Alternatively, we could swap out only safe threads. 107891116Smsmith */ 107991116Smsmith FOREACH_THREAD_IN_PROC(p, td) { 108091116Smsmith KASSERT(thread_safetoswapout(td), 108191116Smsmith ("swapout: there is a thread not safe for swapout")); 108291116Smsmith } 1083193267Sjkim#endif /* INVARIANTS */ 108491116Smsmith 108591116Smsmith ++p->p_stats->p_ru.ru_nswap; 108691116Smsmith /* 1087167802Sjkim * remember the process resident count 108891116Smsmith */ 108991116Smsmith p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 1090193267Sjkim 109191116Smsmith p->p_sflag &= ~PS_INMEM; 109291116Smsmith p->p_sflag |= PS_SWAPPINGOUT; 109367754Smsmith PROC_UNLOCK(p); 109491116Smsmith FOREACH_THREAD_IN_PROC(p, td) 109567754Smsmith TD_SET_SWAPPED(td); 109667754Smsmith mtx_unlock_spin(&sched_lock); 1097193267Sjkim 109867754Smsmith vm_proc_swapout(p); 109999679Siwasaki FOREACH_THREAD_IN_PROC(p, td) 110067754Smsmith vm_thread_swapout(td); 110177424Smsmith 110267754Smsmith PROC_LOCK(p); 110367754Smsmith mtx_lock_spin(&sched_lock); 110467754Smsmith p->p_sflag &= ~PS_SWAPPINGOUT; 110584491Smsmith p->p_swtime = 0; 110667754Smsmith} 1107193267Sjkim#endif /* !NO_SWAPPING */ 1108167802Sjkim