1139825Simp/*- 21541Srgrimes * Copyright (c) 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * The Mach Operating System project at Carnegie-Mellon University. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 4. Neither the name of the University nor the names of its contributors 171541Srgrimes * may be used to endorse or promote products derived from this software 181541Srgrimes * without specific prior written permission. 191541Srgrimes * 201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301541Srgrimes * SUCH DAMAGE. 311541Srgrimes * 321817Sdg * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 331541Srgrimes * 341541Srgrimes * 351541Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 361541Srgrimes * All rights reserved. 375455Sdg * 381541Srgrimes * Permission to use, copy, modify and distribute this software and 391541Srgrimes * its documentation is hereby granted, provided that both the copyright 401541Srgrimes * notice and this permission notice appear in all copies of the 411541Srgrimes * software, derivative works or modified versions, and any portions 421541Srgrimes * thereof, and that both notices appear in supporting documentation. 435455Sdg * 445455Sdg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 455455Sdg * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 461541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 475455Sdg * 481541Srgrimes * Carnegie Mellon requests users of this software to return to 491541Srgrimes * 501541Srgrimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 511541Srgrimes * School of Computer Science 521541Srgrimes * Carnegie Mellon University 531541Srgrimes * Pittsburgh PA 15213-3890 541541Srgrimes * 551541Srgrimes * any improvements or extensions that they make and grant Carnegie the 561541Srgrimes * rights to redistribute these changes. 571541Srgrimes */ 581541Srgrimes 59116226Sobrien#include <sys/cdefs.h> 60116226Sobrien__FBSDID("$FreeBSD$"); 61116226Sobrien 6239770Sabial#include "opt_vm.h" 63118234Speter#include "opt_kstack_pages.h" 64118234Speter#include "opt_kstack_max_pages.h" 65272946Skib#include "opt_kstack_usage_prof.h" 6620821Sjoerg 671541Srgrimes#include <sys/param.h> 681541Srgrimes#include <sys/systm.h> 69114216Skan#include <sys/limits.h> 7076166Smarkm#include <sys/lock.h> 71254025Sjeff#include <sys/malloc.h> 7276166Smarkm#include <sys/mutex.h> 731541Srgrimes#include <sys/proc.h> 74220373Strasz#include <sys/racct.h> 751541Srgrimes#include <sys/resourcevar.h> 76248084Sattilio#include <sys/rwlock.h> 77153485Salc#include <sys/sched.h> 78153485Salc#include <sys/sf_buf.h> 797430Sbde#include <sys/shm.h> 8012662Sdg#include <sys/vmmeter.h> 81254025Sjeff#include <sys/vmem.h> 8274927Sjhb#include <sys/sx.h> 8331563Sdyson#include <sys/sysctl.h> 84228567Skib#include <sys/_kstack_cache.h> 85196730Skib#include <sys/eventhandler.h> 861549Srgrimes#include <sys/kernel.h> 8765557Sjasone#include <sys/ktr.h> 8824691Speter#include <sys/unistd.h> 891549Srgrimes 901541Srgrimes#include <vm/vm.h> 9112662Sdg#include <vm/vm_param.h> 9212662Sdg#include <vm/pmap.h> 9312662Sdg#include <vm/vm_map.h> 941541Srgrimes#include <vm/vm_page.h> 951549Srgrimes#include <vm/vm_pageout.h> 9699559Speter#include <vm/vm_object.h> 971541Srgrimes#include <vm/vm_kern.h> 9812662Sdg#include <vm/vm_extern.h> 9999559Speter#include <vm/vm_pager.h> 100107913Sdillon#include <vm/swap_pager.h> 1011541Srgrimes 102272946Skib#include <machine/cpu.h> 103272946Skib 10489464Sbde#ifndef NO_SWAPPING 105172207Sjeffstatic int swapout(struct proc *); 106172207Sjeffstatic void swapclear(struct proc *); 107206483Salcstatic void vm_thread_swapin(struct thread *td); 108206483Salcstatic void vm_thread_swapout(struct thread *td); 10989464Sbde#endif 11012820Sphk 11198600Salc/* 11298600Salc * MPSAFE 113109572Sdillon * 114109572Sdillon * WARNING! This code calls vm_map_check_protection() which only checks 115109572Sdillon * the associated vm_map_entry range. It does not determine whether the 116109572Sdillon * contents of the memory is actually readable or writable. In most cases 117109572Sdillon * just checking the vm_map_entry is sufficient within the kernel's address 118109572Sdillon * space. 11998600Salc */ 1201541Srgrimesint 1211541Srgrimeskernacc(addr, len, rw) 122109630Salfred void *addr; 1231541Srgrimes int len, rw; 1241541Srgrimes{ 1251541Srgrimes boolean_t rv; 1261541Srgrimes vm_offset_t saddr, eaddr; 12752644Sphk vm_prot_t prot; 1281541Srgrimes 12989464Sbde KASSERT((rw & ~VM_PROT_ALL) == 0, 13052644Sphk ("illegal ``rw'' argument to kernacc (%x)\n", rw)); 131140622Salc 132140622Salc if ((vm_offset_t)addr + len > kernel_map->max_offset || 133140622Salc (vm_offset_t)addr + len < (vm_offset_t)addr) 134140622Salc return (FALSE); 135140622Salc 13652644Sphk prot = rw; 13740286Sdg saddr = trunc_page((vm_offset_t)addr); 13840286Sdg eaddr = round_page((vm_offset_t)addr + len); 139122384Salc vm_map_lock_read(kernel_map); 1401541Srgrimes rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 141122384Salc vm_map_unlock_read(kernel_map); 1425455Sdg return (rv == TRUE); 1431541Srgrimes} 1441541Srgrimes 14598600Salc/* 14698600Salc * MPSAFE 147109572Sdillon * 148109572Sdillon * WARNING! This code calls vm_map_check_protection() which only checks 149109572Sdillon * the associated vm_map_entry range. It does not determine whether the 150109572Sdillon * contents of the memory is actually readable or writable. vmapbuf(), 151109572Sdillon * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be 152109572Sdillon * used in conjuction with this call. 15398600Salc */ 1541541Srgrimesint 1551541Srgrimesuseracc(addr, len, rw) 156109630Salfred void *addr; 1571541Srgrimes int len, rw; 1581541Srgrimes{ 1591541Srgrimes boolean_t rv; 16052644Sphk vm_prot_t prot; 161103767Sjake vm_map_t map; 1621541Srgrimes 16389464Sbde KASSERT((rw & ~VM_PROT_ALL) == 0, 16452644Sphk ("illegal ``rw'' argument to useracc (%x)\n", rw)); 16552644Sphk prot = rw; 166103767Sjake map = &curproc->p_vmspace->vm_map; 167103767Sjake if ((vm_offset_t)addr + len > vm_map_max(map) || 168103767Sjake (vm_offset_t)addr + len < (vm_offset_t)addr) { 1691549Srgrimes return (FALSE); 1701549Srgrimes } 171122384Salc vm_map_lock_read(map); 172103767Sjake rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr), 173103767Sjake round_page((vm_offset_t)addr + len), prot); 174122384Salc vm_map_unlock_read(map); 1755455Sdg return (rv == TRUE); 1761541Srgrimes} 1771541Srgrimes 178126668Struckmanint 179127008Struckmanvslock(void *addr, size_t len) 180126668Struckman{ 181127013Struckman vm_offset_t end, last, start; 182127013Struckman vm_size_t npages; 183127013Struckman int error; 184126668Struckman 185127013Struckman last = (vm_offset_t)addr + len; 186127007Struckman start = trunc_page((vm_offset_t)addr); 187127013Struckman end = round_page(last); 188127013Struckman if (last < (vm_offset_t)addr || end < (vm_offset_t)addr) 189126668Struckman return (EINVAL); 190126668Struckman npages = atop(end - start); 191126668Struckman if (npages > vm_page_max_wired) 192126668Struckman return (ENOMEM); 193126668Struckman#if 0 194126668Struckman /* 195126668Struckman * XXX - not yet 196126668Struckman * 197126668Struckman * The limit for transient usage of wired pages should be 198126668Struckman * larger than for "permanent" wired pages (mlock()). 199126668Struckman * 200126668Struckman * Also, the sysctl code, which is the only present user 201126668Struckman * of vslock(), does a hard loop on EAGAIN. 202126668Struckman */ 203170170Sattilio if (npages + cnt.v_wire_count > vm_page_max_wired) 204126668Struckman return (EAGAIN); 205126668Struckman#endif 206127007Struckman error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end, 207129028Sgreen VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 208127007Struckman /* 209127007Struckman * Return EFAULT on error to match copy{in,out}() behaviour 210127007Struckman * rather than returning ENOMEM like mlock() would. 211127007Struckman */ 212127007Struckman return (error == KERN_SUCCESS ? 0 : EFAULT); 213126668Struckman} 214126668Struckman 215127007Struckmanvoid 216127008Struckmanvsunlock(void *addr, size_t len) 217126668Struckman{ 218126668Struckman 219127007Struckman /* Rely on the parameter sanity checks performed by vslock(). */ 220127007Struckman (void)vm_map_unwire(&curproc->p_vmspace->vm_map, 221127007Struckman trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), 222127008Struckman VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 223126668Struckman} 224126668Struckman 225153485Salc/* 226153485Salc * Pin the page contained within the given object at the given offset. If the 227153485Salc * page is not resident, allocate and load it using the given object's pager. 228153485Salc * Return the pinned page if successful; otherwise, return NULL. 229153485Salc */ 230153485Salcstatic vm_page_t 231253953Sattiliovm_imgact_hold_page(vm_object_t object, vm_ooffset_t offset) 232153485Salc{ 233153485Salc vm_page_t m, ma[1]; 234153485Salc vm_pindex_t pindex; 235153485Salc int rv; 236153485Salc 237248084Sattilio VM_OBJECT_WLOCK(object); 238153485Salc pindex = OFF_TO_IDX(offset); 239254649Skib m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL); 240193643Salc if (m->valid != VM_PAGE_BITS_ALL) { 241153485Salc ma[0] = m; 242153485Salc rv = vm_pager_get_pages(object, ma, 1, 0); 243153485Salc m = vm_page_lookup(object, pindex); 244153485Salc if (m == NULL) 245153485Salc goto out; 246193593Salc if (rv != VM_PAGER_OK) { 247207410Skmacy vm_page_lock(m); 248153485Salc vm_page_free(m); 249207410Skmacy vm_page_unlock(m); 250153485Salc m = NULL; 251153485Salc goto out; 252153485Salc } 253153485Salc } 254254138Sattilio vm_page_xunbusy(m); 255253953Sattilio vm_page_lock(m); 256253953Sattilio vm_page_hold(m); 257270205Skib vm_page_activate(m); 258253953Sattilio vm_page_unlock(m); 259153485Salcout: 260248084Sattilio VM_OBJECT_WUNLOCK(object); 261153485Salc return (m); 262153485Salc} 263153485Salc 264153485Salc/* 265153485Salc * Return a CPU private mapping to the page at the given offset within the 266153485Salc * given object. The page is pinned before it is mapped. 267153485Salc */ 268153485Salcstruct sf_buf * 269153485Salcvm_imgact_map_page(vm_object_t object, vm_ooffset_t offset) 270153485Salc{ 271153485Salc vm_page_t m; 272153485Salc 273253953Sattilio m = vm_imgact_hold_page(object, offset); 274153485Salc if (m == NULL) 275153485Salc return (NULL); 276153485Salc sched_pin(); 277153485Salc return (sf_buf_alloc(m, SFB_CPUPRIVATE)); 278153485Salc} 279153485Salc 280153485Salc/* 281153485Salc * Destroy the given CPU private mapping and unpin the page that it mapped. 282153485Salc */ 283153485Salcvoid 284253953Sattiliovm_imgact_unmap_page(struct sf_buf *sf) 285153485Salc{ 286153485Salc vm_page_t m; 287153485Salc 288153485Salc m = sf_buf_page(sf); 289153485Salc sf_buf_free(sf); 290153485Salc sched_unpin(); 291253953Sattilio vm_page_lock(m); 292253953Sattilio vm_page_unhold(m); 293253953Sattilio vm_page_unlock(m); 294153485Salc} 295153485Salc 296198341Smarcelvoid 297198341Smarcelvm_sync_icache(vm_map_t map, vm_offset_t va, vm_offset_t sz) 298198341Smarcel{ 299198341Smarcel 300198341Smarcel pmap_sync_icache(map->pmap, va, sz); 301198341Smarcel} 302198341Smarcel 303228567Skibstruct kstack_cache_entry *kstack_cache; 304196730Skibstatic int kstack_cache_size = 128; 305196730Skibstatic int kstacks; 306196730Skibstatic struct mtx kstack_cache_mtx; 307242151SandreMTX_SYSINIT(kstack_cache, &kstack_cache_mtx, "kstkch", MTX_DEF); 308242151Sandre 309196730SkibSYSCTL_INT(_vm, OID_AUTO, kstack_cache_size, CTLFLAG_RW, &kstack_cache_size, 0, 310196730Skib ""); 311196730SkibSYSCTL_INT(_vm, OID_AUTO, kstacks, CTLFLAG_RD, &kstacks, 0, 312196730Skib ""); 313196730Skib 314116355Salc#ifndef KSTACK_MAX_PAGES 315116355Salc#define KSTACK_MAX_PAGES 32 316116355Salc#endif 317116355Salc 31899559Speter/* 319116355Salc * Create the kernel stack (including pcb for i386) for a new thread. 320116355Salc * This routine directly affects the fork perf for a process and 321116355Salc * create performance for a thread. 322116355Salc */ 323173361Skibint 324116355Salcvm_thread_new(struct thread *td, int pages) 325116355Salc{ 326116355Salc vm_object_t ksobj; 327116355Salc vm_offset_t ks; 328116355Salc vm_page_t m, ma[KSTACK_MAX_PAGES]; 329196730Skib struct kstack_cache_entry *ks_ce; 330116355Salc int i; 331116355Salc 332116355Salc /* Bounds check */ 333116355Salc if (pages <= 1) 334116355Salc pages = KSTACK_PAGES; 335116355Salc else if (pages > KSTACK_MAX_PAGES) 336116355Salc pages = KSTACK_MAX_PAGES; 337196730Skib 338196730Skib if (pages == KSTACK_PAGES) { 339196730Skib mtx_lock(&kstack_cache_mtx); 340196730Skib if (kstack_cache != NULL) { 341196730Skib ks_ce = kstack_cache; 342196730Skib kstack_cache = ks_ce->next_ks_entry; 343196730Skib mtx_unlock(&kstack_cache_mtx); 344196730Skib 345196730Skib td->td_kstack_obj = ks_ce->ksobj; 346196730Skib td->td_kstack = (vm_offset_t)ks_ce; 347196730Skib td->td_kstack_pages = KSTACK_PAGES; 348196730Skib return (1); 349196730Skib } 350196730Skib mtx_unlock(&kstack_cache_mtx); 351196730Skib } 352196730Skib 353116355Salc /* 354116355Salc * Allocate an object for the kstack. 355116355Salc */ 356116355Salc ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 357177368Sjeff 358116355Salc /* 359116355Salc * Get a kernel virtual address for this thread's kstack. 360116355Salc */ 361206819Sjmallett#if defined(__mips__) 362206819Sjmallett /* 363206819Sjmallett * We need to align the kstack's mapped address to fit within 364206819Sjmallett * a single TLB entry. 365206819Sjmallett */ 366254025Sjeff if (vmem_xalloc(kernel_arena, (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE, 367254025Sjeff PAGE_SIZE * 2, 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX, 368254025Sjeff M_BESTFIT | M_NOWAIT, &ks)) { 369254025Sjeff ks = 0; 370254025Sjeff } 371206819Sjmallett#else 372254025Sjeff ks = kva_alloc((pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 373206819Sjmallett#endif 374173361Skib if (ks == 0) { 375173361Skib printf("vm_thread_new: kstack allocation failed\n"); 376173361Skib vm_object_deallocate(ksobj); 377173361Skib return (0); 378173361Skib } 379196730Skib 380196730Skib atomic_add_int(&kstacks, 1); 381116355Salc if (KSTACK_GUARD_PAGES != 0) { 382116355Salc pmap_qremove(ks, KSTACK_GUARD_PAGES); 383116355Salc ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 384116355Salc } 385173361Skib td->td_kstack_obj = ksobj; 386116355Salc td->td_kstack = ks; 387116355Salc /* 388116355Salc * Knowing the number of pages allocated is useful when you 389116355Salc * want to deallocate them. 390116355Salc */ 391116355Salc td->td_kstack_pages = pages; 392116355Salc /* 393116355Salc * For the length of the stack, link in a real page of ram for each 394116355Salc * page of stack. 395116355Salc */ 396248084Sattilio VM_OBJECT_WLOCK(ksobj); 397116355Salc for (i = 0; i < pages; i++) { 398116355Salc /* 399116355Salc * Get a kernel stack page. 400116355Salc */ 401136923Salc m = vm_page_grab(ksobj, i, VM_ALLOC_NOBUSY | 402254649Skib VM_ALLOC_NORMAL | VM_ALLOC_WIRED); 403116355Salc ma[i] = m; 404116355Salc m->valid = VM_PAGE_BITS_ALL; 405116355Salc } 406248084Sattilio VM_OBJECT_WUNLOCK(ksobj); 407116355Salc pmap_qenter(ks, ma, pages); 408173361Skib return (1); 409116355Salc} 410116355Salc 411196730Skibstatic void 412196730Skibvm_thread_stack_dispose(vm_object_t ksobj, vm_offset_t ks, int pages) 413116355Salc{ 414116355Salc vm_page_t m; 415196730Skib int i; 416116355Salc 417196730Skib atomic_add_int(&kstacks, -1); 418116355Salc pmap_qremove(ks, pages); 419248084Sattilio VM_OBJECT_WLOCK(ksobj); 420116355Salc for (i = 0; i < pages; i++) { 421116355Salc m = vm_page_lookup(ksobj, i); 422116355Salc if (m == NULL) 423116355Salc panic("vm_thread_dispose: kstack already missing?"); 424207410Skmacy vm_page_lock(m); 425116355Salc vm_page_unwire(m, 0); 426116355Salc vm_page_free(m); 427207410Skmacy vm_page_unlock(m); 428116355Salc } 429248084Sattilio VM_OBJECT_WUNLOCK(ksobj); 430116355Salc vm_object_deallocate(ksobj); 431254025Sjeff kva_free(ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 432116355Salc (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 433196730Skib} 434196730Skib 435196730Skib/* 436196730Skib * Dispose of a thread's kernel stack. 437196730Skib */ 438196730Skibvoid 439196730Skibvm_thread_dispose(struct thread *td) 440196730Skib{ 441196730Skib vm_object_t ksobj; 442196730Skib vm_offset_t ks; 443196730Skib struct kstack_cache_entry *ks_ce; 444196730Skib int pages; 445196730Skib 446196730Skib pages = td->td_kstack_pages; 447196730Skib ksobj = td->td_kstack_obj; 448196730Skib ks = td->td_kstack; 449173361Skib td->td_kstack = 0; 450196730Skib td->td_kstack_pages = 0; 451196730Skib if (pages == KSTACK_PAGES && kstacks <= kstack_cache_size) { 452196730Skib ks_ce = (struct kstack_cache_entry *)ks; 453196730Skib ks_ce->ksobj = ksobj; 454196730Skib mtx_lock(&kstack_cache_mtx); 455196730Skib ks_ce->next_ks_entry = kstack_cache; 456196730Skib kstack_cache = ks_ce; 457196730Skib mtx_unlock(&kstack_cache_mtx); 458196730Skib return; 459196730Skib } 460196730Skib vm_thread_stack_dispose(ksobj, ks, pages); 461116355Salc} 462116355Salc 463196730Skibstatic void 464196730Skibvm_thread_stack_lowmem(void *nulll) 465196730Skib{ 466196730Skib struct kstack_cache_entry *ks_ce, *ks_ce1; 467196730Skib 468196730Skib mtx_lock(&kstack_cache_mtx); 469196730Skib ks_ce = kstack_cache; 470196730Skib kstack_cache = NULL; 471196730Skib mtx_unlock(&kstack_cache_mtx); 472196730Skib 473196730Skib while (ks_ce != NULL) { 474196730Skib ks_ce1 = ks_ce; 475196730Skib ks_ce = ks_ce->next_ks_entry; 476196730Skib 477196730Skib vm_thread_stack_dispose(ks_ce1->ksobj, (vm_offset_t)ks_ce1, 478196730Skib KSTACK_PAGES); 479196730Skib } 480196730Skib} 481196730Skib 482196730Skibstatic void 483196730Skibkstack_cache_init(void *nulll) 484196730Skib{ 485196730Skib 486196730Skib EVENTHANDLER_REGISTER(vm_lowmem, vm_thread_stack_lowmem, NULL, 487196730Skib EVENTHANDLER_PRI_ANY); 488196730Skib} 489196730Skib 490196730SkibSYSINIT(vm_kstacks, SI_SUB_KTHREAD_INIT, SI_ORDER_ANY, kstack_cache_init, NULL); 491196730Skib 492272946Skib#ifdef KSTACK_USAGE_PROF 493272946Skib/* 494272946Skib * Track maximum stack used by a thread in kernel. 495272946Skib */ 496272946Skibstatic int max_kstack_used; 497272946Skib 498272946SkibSYSCTL_INT(_debug, OID_AUTO, max_kstack_used, CTLFLAG_RD, 499272946Skib &max_kstack_used, 0, 500272946Skib "Maxiumum stack depth used by a thread in kernel"); 501272946Skib 502272946Skibvoid 503272946Skibintr_prof_stack_use(struct thread *td, struct trapframe *frame) 504272946Skib{ 505272946Skib vm_offset_t stack_top; 506272946Skib vm_offset_t current; 507272946Skib int used, prev_used; 508272946Skib 509272946Skib /* 510272946Skib * Testing for interrupted kernel mode isn't strictly 511272946Skib * needed. It optimizes the execution, since interrupts from 512272946Skib * usermode will have only the trap frame on the stack. 513272946Skib */ 514272946Skib if (TRAPF_USERMODE(frame)) 515272946Skib return; 516272946Skib 517272946Skib stack_top = td->td_kstack + td->td_kstack_pages * PAGE_SIZE; 518272946Skib current = (vm_offset_t)(uintptr_t)&stack_top; 519272946Skib 520272946Skib /* 521272946Skib * Try to detect if interrupt is using kernel thread stack. 522272946Skib * Hardware could use a dedicated stack for interrupt handling. 523272946Skib */ 524272946Skib if (stack_top <= current || current < td->td_kstack) 525272946Skib return; 526272946Skib 527272946Skib used = stack_top - current; 528272946Skib for (;;) { 529272946Skib prev_used = max_kstack_used; 530272946Skib if (prev_used >= used) 531272946Skib break; 532272946Skib if (atomic_cmpset_int(&max_kstack_used, prev_used, used)) 533272946Skib break; 534272946Skib } 535272946Skib} 536272946Skib#endif /* KSTACK_USAGE_PROF */ 537272946Skib 538206483Salc#ifndef NO_SWAPPING 539116355Salc/* 540116355Salc * Allow a thread's kernel stack to be paged out. 541116355Salc */ 542206483Salcstatic void 543116355Salcvm_thread_swapout(struct thread *td) 544116355Salc{ 545116355Salc vm_object_t ksobj; 546116355Salc vm_page_t m; 547116355Salc int i, pages; 548116355Salc 549119004Smarcel cpu_thread_swapout(td); 550116355Salc pages = td->td_kstack_pages; 551116355Salc ksobj = td->td_kstack_obj; 552116355Salc pmap_qremove(td->td_kstack, pages); 553248084Sattilio VM_OBJECT_WLOCK(ksobj); 554116355Salc for (i = 0; i < pages; i++) { 555116355Salc m = vm_page_lookup(ksobj, i); 556116355Salc if (m == NULL) 557116355Salc panic("vm_thread_swapout: kstack already missing?"); 558206823Salc vm_page_dirty(m); 559207410Skmacy vm_page_lock(m); 560116355Salc vm_page_unwire(m, 0); 561207410Skmacy vm_page_unlock(m); 562116355Salc } 563248084Sattilio VM_OBJECT_WUNLOCK(ksobj); 564116355Salc} 565116355Salc 566116355Salc/* 567116355Salc * Bring the kernel stack for a specified thread back in. 568116355Salc */ 569206483Salcstatic void 570116355Salcvm_thread_swapin(struct thread *td) 571116355Salc{ 572116355Salc vm_object_t ksobj; 573207365Skib vm_page_t ma[KSTACK_MAX_PAGES]; 574207365Skib int i, j, k, pages, rv; 575116355Salc 576116355Salc pages = td->td_kstack_pages; 577116355Salc ksobj = td->td_kstack_obj; 578248084Sattilio VM_OBJECT_WLOCK(ksobj); 579207365Skib for (i = 0; i < pages; i++) 580254649Skib ma[i] = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | 581207365Skib VM_ALLOC_WIRED); 582116355Salc for (i = 0; i < pages; i++) { 583207365Skib if (ma[i]->valid != VM_PAGE_BITS_ALL) { 584254138Sattilio vm_page_assert_xbusied(ma[i]); 585207365Skib vm_object_pip_add(ksobj, 1); 586207365Skib for (j = i + 1; j < pages; j++) { 587254138Sattilio if (ma[j]->valid != VM_PAGE_BITS_ALL) 588254138Sattilio vm_page_assert_xbusied(ma[j]); 589207365Skib if (ma[j]->valid == VM_PAGE_BITS_ALL) 590207365Skib break; 591207365Skib } 592207365Skib rv = vm_pager_get_pages(ksobj, ma + i, j - i, 0); 593116355Salc if (rv != VM_PAGER_OK) 594207365Skib panic("vm_thread_swapin: cannot get kstack for proc: %d", 595207365Skib td->td_proc->p_pid); 596207365Skib vm_object_pip_wakeup(ksobj); 597207365Skib for (k = i; k < j; k++) 598207365Skib ma[k] = vm_page_lookup(ksobj, k); 599254138Sattilio vm_page_xunbusy(ma[i]); 600254138Sattilio } else if (vm_page_xbusied(ma[i])) 601254138Sattilio vm_page_xunbusy(ma[i]); 602116355Salc } 603248084Sattilio VM_OBJECT_WUNLOCK(ksobj); 604116355Salc pmap_qenter(td->td_kstack, ma, pages); 605119004Smarcel cpu_thread_swapin(td); 606116355Salc} 607206483Salc#endif /* !NO_SWAPPING */ 608116355Salc 609116355Salc/* 6101541Srgrimes * Implement fork's actions on an address space. 6111541Srgrimes * Here we arrange for the address space to be copied or referenced, 6121541Srgrimes * allocate a user struct (pcb and kernel stack), then call the 6131541Srgrimes * machine-dependent layer to fill those in and make the new process 61424691Speter * ready to run. The new process is set up so that it returns directly 61524691Speter * to user mode to avoid stack copying and relocation problems. 6161541Srgrimes */ 617173361Skibint 618173361Skibvm_forkproc(td, p2, td2, vm2, flags) 61983366Sjulian struct thread *td; 62083366Sjulian struct proc *p2; 62190361Sjulian struct thread *td2; 622173361Skib struct vmspace *vm2; 62324691Speter int flags; 6241541Srgrimes{ 62583366Sjulian struct proc *p1 = td->td_proc; 626173361Skib int error; 6271541Srgrimes 62854188Sluoqi if ((flags & RFPROC) == 0) { 62954188Sluoqi /* 63054188Sluoqi * Divorce the memory, if it is shared, essentially 63154188Sluoqi * this changes shared memory amongst threads, into 63254188Sluoqi * COW locally. 63354188Sluoqi */ 63454188Sluoqi if ((flags & RFMEM) == 0) { 63554188Sluoqi if (p1->p_vmspace->vm_refcnt > 1) { 636173361Skib error = vmspace_unshare(p1); 637173361Skib if (error) 638173361Skib return (error); 63954188Sluoqi } 64054188Sluoqi } 64190361Sjulian cpu_fork(td, p2, td2, flags); 642173361Skib return (0); 64354188Sluoqi } 64454188Sluoqi 64524848Sdyson if (flags & RFMEM) { 64624848Sdyson p2->p_vmspace = p1->p_vmspace; 647132684Salc atomic_add_int(&p1->p_vmspace->vm_refcnt, 1); 64824848Sdyson } 64924848Sdyson 65051337Sdillon while (vm_page_count_severe()) { 6511549Srgrimes VM_WAIT; 6525455Sdg } 6531549Srgrimes 65424848Sdyson if ((flags & RFMEM) == 0) { 655173361Skib p2->p_vmspace = vm2; 65624691Speter if (p1->p_vmspace->vm_shm) 65724691Speter shmfork(p1, p2); 65824691Speter } 6591541Srgrimes 66015018Sdyson /* 66124691Speter * cpu_fork will copy and update the pcb, set up the kernel stack, 66224691Speter * and make the child ready to run. 6631541Srgrimes */ 66490361Sjulian cpu_fork(td, p2, td2, flags); 665173361Skib return (0); 6661541Srgrimes} 6671541Srgrimes 6681541Srgrimes/* 66983276Speter * Called after process has been wait(2)'ed apon and is being reaped. 67083276Speter * The idea is to reclaim resources that we could not reclaim while 67183276Speter * the process was still executing. 67283276Speter */ 67383276Spetervoid 67483276Spetervm_waitproc(p) 67583276Speter struct proc *p; 67683276Speter{ 67783276Speter 67890263Salfred vmspace_exitfree(p); /* and clean-out the vmspace */ 67983276Speter} 68083276Speter 6811549Srgrimesvoid 6821549Srgrimesfaultin(p) 6835455Sdg struct proc *p; 6841549Srgrimes{ 685113918Sjhb#ifdef NO_SWAPPING 686113918Sjhb 687113918Sjhb PROC_LOCK_ASSERT(p, MA_OWNED); 688172207Sjeff if ((p->p_flag & P_INMEM) == 0) 689113918Sjhb panic("faultin: proc swapped out with NO_SWAPPING!"); 690113918Sjhb#else /* !NO_SWAPPING */ 691113867Sjhb struct thread *td; 69299559Speter 69376641Sjhb PROC_LOCK_ASSERT(p, MA_OWNED); 694113867Sjhb /* 695113867Sjhb * If another process is swapping in this process, 696113867Sjhb * just wait until it finishes. 697113867Sjhb */ 698172207Sjeff if (p->p_flag & P_SWAPPINGIN) { 699172207Sjeff while (p->p_flag & P_SWAPPINGIN) 700172207Sjeff msleep(&p->p_flag, &p->p_mtx, PVM, "faultin", 0); 701172207Sjeff return; 702172207Sjeff } 703172207Sjeff if ((p->p_flag & P_INMEM) == 0) { 704100884Sjulian /* 705113867Sjhb * Don't let another thread swap process p out while we are 706113867Sjhb * busy swapping it in. 707100884Sjulian */ 708113867Sjhb ++p->p_lock; 709172207Sjeff p->p_flag |= P_SWAPPINGIN; 71071610Sjhb PROC_UNLOCK(p); 7111549Srgrimes 712172207Sjeff /* 713172207Sjeff * We hold no lock here because the list of threads 714172207Sjeff * can not change while all threads in the process are 715172207Sjeff * swapped out. 716172207Sjeff */ 717113867Sjhb FOREACH_THREAD_IN_PROC(p, td) 718116355Salc vm_thread_swapin(td); 71971610Sjhb PROC_LOCK(p); 720172207Sjeff swapclear(p); 721172268Sjeff p->p_swtick = ticks; 7221549Srgrimes 723172207Sjeff wakeup(&p->p_flag); 7241549Srgrimes 725113867Sjhb /* Allow other threads to swap p out now. */ 7261549Srgrimes --p->p_lock; 7271549Srgrimes } 728113918Sjhb#endif /* NO_SWAPPING */ 7295455Sdg} 7301549Srgrimes 7311541Srgrimes/* 7321549Srgrimes * This swapin algorithm attempts to swap-in processes only if there 7331549Srgrimes * is enough space for them. Of course, if a process waits for a long 7341549Srgrimes * time, it will be swapped in anyway. 7351541Srgrimes */ 736253604Savgvoid 737253604Savgswapper(void) 7381541Srgrimes{ 73979242Sdillon struct proc *p; 74099072Sjulian struct thread *td; 7411541Srgrimes struct proc *pp; 742172268Sjeff int slptime; 743172268Sjeff int swtime; 7441541Srgrimes int ppri; 745172268Sjeff int pri; 7461541Srgrimes 7471541Srgrimesloop: 74851337Sdillon if (vm_page_count_min()) { 7495455Sdg VM_WAIT; 75051337Sdillon goto loop; 7515455Sdg } 7521549Srgrimes 7531541Srgrimes pp = NULL; 7541541Srgrimes ppri = INT_MIN; 75574927Sjhb sx_slock(&allproc_lock); 75683366Sjulian FOREACH_PROC_IN_SYSTEM(p) { 757172207Sjeff PROC_LOCK(p); 758220390Sjhb if (p->p_state == PRS_NEW || 759220390Sjhb p->p_flag & (P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) { 760172207Sjeff PROC_UNLOCK(p); 76199072Sjulian continue; 76299072Sjulian } 763172268Sjeff swtime = (ticks - p->p_swtick) / hz; 76499072Sjulian FOREACH_THREAD_IN_PROC(p, td) { 765100884Sjulian /* 766103216Sjulian * An otherwise runnable thread of a process 767103216Sjulian * swapped out has only the TDI_SWAPPED bit set. 768103216Sjulian * 769100884Sjulian */ 770170307Sjeff thread_lock(td); 771103216Sjulian if (td->td_inhibitors == TDI_SWAPPED) { 772172268Sjeff slptime = (ticks - td->td_slptick) / hz; 773172268Sjeff pri = swtime + slptime; 774172207Sjeff if ((td->td_flags & TDF_SWAPINREQ) == 0) 775130551Sjulian pri -= p->p_nice * 8; 77683366Sjulian /* 777164936Sjulian * if this thread is higher priority 77883366Sjulian * and there is enough space, then select 77983366Sjulian * this process instead of the previous 78083366Sjulian * selection. 78183366Sjulian */ 78283366Sjulian if (pri > ppri) { 78383366Sjulian pp = p; 78483366Sjulian ppri = pri; 78583366Sjulian } 78618974Sdyson } 787170307Sjeff thread_unlock(td); 7881541Srgrimes } 789172207Sjeff PROC_UNLOCK(p); 7901541Srgrimes } 79174927Sjhb sx_sunlock(&allproc_lock); 7921549Srgrimes 7931541Srgrimes /* 79418974Sdyson * Nothing to do, back to sleep. 7951541Srgrimes */ 7961541Srgrimes if ((p = pp) == NULL) { 797253604Savg tsleep(&proc0, PVM, "swapin", MAXSLP * hz / 2); 7981541Srgrimes goto loop; 7991541Srgrimes } 800100884Sjulian PROC_LOCK(p); 801100884Sjulian 802100884Sjulian /* 803100884Sjulian * Another process may be bringing or may have already 804100884Sjulian * brought this process in while we traverse all threads. 805100884Sjulian * Or, this process may even be being swapped out again. 806100884Sjulian */ 807172207Sjeff if (p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) { 808100884Sjulian PROC_UNLOCK(p); 809100884Sjulian goto loop; 810100884Sjulian } 811100884Sjulian 8121541Srgrimes /* 8131549Srgrimes * We would like to bring someone in. (only if there is space). 81499072Sjulian * [What checks the space? ] 8151541Srgrimes */ 8165455Sdg faultin(p); 81771610Sjhb PROC_UNLOCK(p); 8181541Srgrimes goto loop; 8191541Srgrimes} 8201541Srgrimes 821181334Sjhbvoid 822181334Sjhbkick_proc0(void) 823146554Sups{ 824146554Sups 825181334Sjhb wakeup(&proc0); 826146554Sups} 827146554Sups 82814178Sdg#ifndef NO_SWAPPING 82914178Sdg 8301541Srgrimes/* 83131563Sdyson * Swap_idle_threshold1 is the guaranteed swapped in time for a process 83231563Sdyson */ 83333181Seivindstatic int swap_idle_threshold1 = 2; 834113600StrhodesSYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW, 835113603Strhodes &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process"); 83631563Sdyson 83731563Sdyson/* 83831563Sdyson * Swap_idle_threshold2 is the time that a process can be idle before 83931563Sdyson * it will be swapped out, if idle swapping is enabled. 84031563Sdyson */ 84133181Seivindstatic int swap_idle_threshold2 = 10; 842113600StrhodesSYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW, 843113603Strhodes &swap_idle_threshold2, 0, "Time before a process will be swapped out"); 84431563Sdyson 84531563Sdyson/* 846217192Skib * First, if any processes have been sleeping or stopped for at least 847217192Skib * "swap_idle_threshold1" seconds, they are swapped out. If, however, 848217192Skib * no such processes exist, then the longest-sleeping or stopped 849217192Skib * process is swapped out. Finally, and only as a last resort, if 850217192Skib * there are no sleeping or stopped processes, the longest-resident 851217192Skib * process is swapped out. 8521541Srgrimes */ 8531541Srgrimesvoid 85431667Sdysonswapout_procs(action) 85531667Sdysonint action; 8561541Srgrimes{ 85779242Sdillon struct proc *p; 85899072Sjulian struct thread *td; 8591541Srgrimes int didswap = 0; 8601541Srgrimes 86177089Sjhbretry: 86274927Sjhb sx_slock(&allproc_lock); 86399072Sjulian FOREACH_PROC_IN_SYSTEM(p) { 86415809Sdyson struct vmspace *vm; 86583366Sjulian int minslptime = 100000; 866172268Sjeff int slptime; 86771570Sjhb 868100913Stanimura /* 869103123Stanimura * Watch out for a process in 870103123Stanimura * creation. It may have no 871105695Sjhb * address space or lock yet. 872105695Sjhb */ 873172207Sjeff if (p->p_state == PRS_NEW) 874105695Sjhb continue; 875105695Sjhb /* 876103123Stanimura * An aio daemon switches its 877103123Stanimura * address space while running. 878103123Stanimura * Perform a quick check whether 879103123Stanimura * a process has P_SYSTEM. 880100913Stanimura */ 881114030Sjhb if ((p->p_flag & P_SYSTEM) != 0) 882103123Stanimura continue; 883105695Sjhb /* 884105695Sjhb * Do not swapout a process that 885105695Sjhb * is waiting for VM data 886105695Sjhb * structures as there is a possible 887105695Sjhb * deadlock. Test this first as 888105695Sjhb * this may block. 889105695Sjhb * 890105695Sjhb * Lock the map until swapout 891105695Sjhb * finishes, or a thread of this 892105695Sjhb * process may attempt to alter 893105695Sjhb * the map. 894105695Sjhb */ 895159054Stegge vm = vmspace_acquire_ref(p); 896159054Stegge if (vm == NULL) 897159054Stegge continue; 898100913Stanimura if (!vm_map_trylock(&vm->vm_map)) 899100913Stanimura goto nextproc1; 900100913Stanimura 90171570Sjhb PROC_LOCK(p); 90271574Sjhb if (p->p_lock != 0 || 903102950Sdavidxu (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT) 904102950Sdavidxu ) != 0) { 905177368Sjeff goto nextproc; 90671570Sjhb } 90776827Salfred /* 90876827Salfred * only aiod changes vmspace, however it will be 90976827Salfred * skipped because of the if statement above checking 91076827Salfred * for P_SYSTEM 91176827Salfred */ 912172207Sjeff if ((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) != P_INMEM) 913177368Sjeff goto nextproc; 914113867Sjhb 91599072Sjulian switch (p->p_state) { 9165455Sdg default: 91799072Sjulian /* Don't swap out processes in any sort 91899072Sjulian * of 'special' state. */ 919114030Sjhb break; 9205455Sdg 92199072Sjulian case PRS_NORMAL: 9221549Srgrimes /* 9235455Sdg * do not swapout a realtime process 92483366Sjulian * Check all the thread groups.. 9255455Sdg */ 926163709Sjb FOREACH_THREAD_IN_PROC(p, td) { 927172207Sjeff thread_lock(td); 928172207Sjeff if (PRI_IS_REALTIME(td->td_pri_class)) { 929172207Sjeff thread_unlock(td); 93083366Sjulian goto nextproc; 931172207Sjeff } 932172268Sjeff slptime = (ticks - td->td_slptick) / hz; 93383366Sjulian /* 934100913Stanimura * Guarantee swap_idle_threshold1 93583366Sjulian * time in memory. 93683366Sjulian */ 937172268Sjeff if (slptime < swap_idle_threshold1) { 938172207Sjeff thread_unlock(td); 93983366Sjulian goto nextproc; 940172207Sjeff } 941100913Stanimura 942100884Sjulian /* 943100913Stanimura * Do not swapout a process if it is 944100913Stanimura * waiting on a critical event of some 945100913Stanimura * kind or there is a thread whose 946100913Stanimura * pageable memory may be accessed. 947100884Sjulian * 948100884Sjulian * This could be refined to support 949100884Sjulian * swapping out a thread. 950100884Sjulian */ 951177085Sjeff if (!thread_safetoswapout(td)) { 952172207Sjeff thread_unlock(td); 953163709Sjb goto nextproc; 954172207Sjeff } 95583366Sjulian /* 95683366Sjulian * If the system is under memory stress, 95783366Sjulian * or if we are swapping 95883366Sjulian * idle processes >= swap_idle_threshold2, 95983366Sjulian * then swap the process out. 96083366Sjulian */ 96183366Sjulian if (((action & VM_SWAP_NORMAL) == 0) && 96283366Sjulian (((action & VM_SWAP_IDLE) == 0) || 963172268Sjeff (slptime < swap_idle_threshold2))) { 964172207Sjeff thread_unlock(td); 96583366Sjulian goto nextproc; 966172207Sjeff } 967100913Stanimura 968172268Sjeff if (minslptime > slptime) 969172268Sjeff minslptime = slptime; 970172207Sjeff thread_unlock(td); 97169509Sjhb } 9725455Sdg 9735455Sdg /* 974131163Sdas * If the pageout daemon didn't free enough pages, 975131163Sdas * or if this process is idle and the system is 976131163Sdas * configured to swap proactively, swap it out. 9772692Sdg */ 97831563Sdyson if ((action & VM_SWAP_NORMAL) || 97931563Sdyson ((action & VM_SWAP_IDLE) && 98083366Sjulian (minslptime > swap_idle_threshold2))) { 981172207Sjeff if (swapout(p) == 0) 982172207Sjeff didswap++; 983113867Sjhb PROC_UNLOCK(p); 984100913Stanimura vm_map_unlock(&vm->vm_map); 98531563Sdyson vmspace_free(vm); 986100913Stanimura sx_sunlock(&allproc_lock); 98731563Sdyson goto retry; 98876641Sjhb } 9891541Srgrimes } 990177368Sjeffnextproc: 991100913Stanimura PROC_UNLOCK(p); 992100913Stanimura vm_map_unlock(&vm->vm_map); 993100913Stanimuranextproc1: 994100913Stanimura vmspace_free(vm); 99592666Speter continue; 9961541Srgrimes } 99774927Sjhb sx_sunlock(&allproc_lock); 9981541Srgrimes /* 9991549Srgrimes * If we swapped something out, and another process needed memory, 10001549Srgrimes * then wakeup the sched process. 10011549Srgrimes */ 10025455Sdg if (didswap) 10039507Sdg wakeup(&proc0); 10041541Srgrimes} 10051541Srgrimes 100612820Sphkstatic void 1007172207Sjeffswapclear(p) 1008172207Sjeff struct proc *p; 1009172207Sjeff{ 1010172207Sjeff struct thread *td; 1011172207Sjeff 1012172207Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 1013172207Sjeff 1014172207Sjeff FOREACH_THREAD_IN_PROC(p, td) { 1015172207Sjeff thread_lock(td); 1016172207Sjeff td->td_flags |= TDF_INMEM; 1017172207Sjeff td->td_flags &= ~TDF_SWAPINREQ; 1018172207Sjeff TD_CLR_SWAPPED(td); 1019172207Sjeff if (TD_CAN_RUN(td)) 1020181334Sjhb if (setrunnable(td)) { 1021181334Sjhb#ifdef INVARIANTS 1022181334Sjhb /* 1023181334Sjhb * XXX: We just cleared TDI_SWAPPED 1024181334Sjhb * above and set TDF_INMEM, so this 1025181334Sjhb * should never happen. 1026181334Sjhb */ 1027181334Sjhb panic("not waking up swapper"); 1028181334Sjhb#endif 1029181334Sjhb } 1030172207Sjeff thread_unlock(td); 1031172207Sjeff } 1032172207Sjeff p->p_flag &= ~(P_SWAPPINGIN|P_SWAPPINGOUT); 1033172207Sjeff p->p_flag |= P_INMEM; 1034172207Sjeff} 1035172207Sjeff 1036172207Sjeffstatic int 10371541Srgrimesswapout(p) 103879242Sdillon struct proc *p; 10391541Srgrimes{ 104083366Sjulian struct thread *td; 10411541Srgrimes 104276778Sjhb PROC_LOCK_ASSERT(p, MA_OWNED); 104315153Sdyson#if defined(SWAP_DEBUG) 104415153Sdyson printf("swapping out %d\n", p->p_pid); 104515153Sdyson#endif 1046100884Sjulian 1047100884Sjulian /* 1048100913Stanimura * The states of this process and its threads may have changed 1049100913Stanimura * by now. Assuming that there is only one pageout daemon thread, 1050100913Stanimura * this process should still be in memory. 1051100913Stanimura */ 1052172207Sjeff KASSERT((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) == P_INMEM, 1053100913Stanimura ("swapout: lost a swapout race?")); 1054100913Stanimura 1055100913Stanimura /* 1056172207Sjeff * remember the process resident count 1057100884Sjulian */ 1058172207Sjeff p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 10591541Srgrimes /* 1060172207Sjeff * Check and mark all threads before we proceed. 10611541Srgrimes */ 1062172207Sjeff p->p_flag &= ~P_INMEM; 1063172207Sjeff p->p_flag |= P_SWAPPINGOUT; 1064170307Sjeff FOREACH_THREAD_IN_PROC(p, td) { 1065170307Sjeff thread_lock(td); 1066172207Sjeff if (!thread_safetoswapout(td)) { 1067172207Sjeff thread_unlock(td); 1068172207Sjeff swapclear(p); 1069172207Sjeff return (EBUSY); 1070172207Sjeff } 1071172207Sjeff td->td_flags &= ~TDF_INMEM; 1072113867Sjhb TD_SET_SWAPPED(td); 1073170307Sjeff thread_unlock(td); 1074170307Sjeff } 1075172207Sjeff td = FIRST_THREAD_IN_PROC(p); 1076172207Sjeff ++td->td_ru.ru_nswap; 1077172207Sjeff PROC_UNLOCK(p); 10781549Srgrimes 1079172207Sjeff /* 1080172207Sjeff * This list is stable because all threads are now prevented from 1081172207Sjeff * running. The list is only modified in the context of a running 1082172207Sjeff * thread in this process. 1083172207Sjeff */ 1084113867Sjhb FOREACH_THREAD_IN_PROC(p, td) 1085116355Salc vm_thread_swapout(td); 1086113867Sjhb 1087113867Sjhb PROC_LOCK(p); 1088172207Sjeff p->p_flag &= ~P_SWAPPINGOUT; 1089172268Sjeff p->p_swtick = ticks; 1090172207Sjeff return (0); 10911541Srgrimes} 109214178Sdg#endif /* !NO_SWAPPING */ 1093